1 / 30

D esign patterns

D esign patterns. What is a design pattern?. Christopher Alexander: « The pattern describes a problem which again and again occurs in the work, as well as the principle of its solution, in a way that the solution can be used repeatedly without changes ».

ovidio
Download Presentation

D esign patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design patterns

  2. What is a design pattern? • Christopher Alexander: «The pattern describes a problem which again and againoccurs in the work, as well as the principle of its solution, in a way thatthe solution can be used repeatedly without changes». • Design patterns is an effective way of the decision of typical problems of designing, in particular design of computer programs. The pattern is not a complete example of a project that can be directly convertedin code, rather it is a description or a sample for how to solve the problem so that it can be used in varioussituations. • Design pattern is a description of the interaction of objects andclasses, adapted for a solution of the general problem of design in a particular context. • Algorithms are not patterns, as they solve the task of calculations rather than a design. • Frameworks of applications are not templates, as they relate to a specific subject area and consist of several patterns.

  3. Reasons to use and classification • Reusability • Use of another's experience instead of self-invention • Unite terminology • The level of abstraction Creational patterns abstract process of instantiation. They allow to make system-independent of a way of creation, composition, and presentation of the objects. Structural patterns solve the question how we can optimally get larger structures from existing classes and objects. Behavioral patternsare responsible for the encapsulationalgorithms and the distribution of responsibilities between objects. There are also concurrency patterns, system patterns, integral patterns etc.

  4. Class image Protected method Public method Private method

  5. Association Customer has a lot of accounts: Class of application uses class of connection:

  6. Generalization Class Circle is an inheritor of a class Shape:

  7. Aggregation Class Customer has a reference to the collection of orders in the field Orders(one-to-many): Class Order has a reference to the product in the field Product(one-to-many):

  8. The scheme of description of the pattern • Definition contains a short answer to the questions: what are the functions andthe purpose of the pattern. • Motivation is a description of situations where you can use the pattern. • Advantages are the benefits of solving the problem with using this pattern. • Class diagram • Participants - description of the classes involved in this pattern design and their functions. • Example(s) of code

  9. Pattern Singleton

  10. Definition and motivation • Singleton is a generating pattern, which guarantees that a class has only oneinstance, and gives a global access point to it. • Motivation: • Must be exactly one instance of some class, easily accessible to all clients. • The only instance should expand by deriving subclasses, and clients need to be able to work with the advanced instance without modification of their code.

  11. Advantages • Controlled access to the single instance.As class “Singleton” encapsulates its onlyinstance, it completely controls how and when clients get access to it. • Decrease the number of names. Pattern avoids clogging of namespace with global variables, which are storing unique instances. • Allows the specification of operations. You can parameterize the application with the instance of that class which is required at run time. • Accepts a variable number of instances. • Greater flexibility than the operations of the class (static methods).

  12. Class diagram

  13. Participants • Singleton specifies the operation to retrieve an instance. It is a static method that allows clients to access the single instance. It may be responsible for creating its own unique instance.

  14. Example 1 Standard implementation:

  15. Example 2 Implementation with the use of templates in C#:

  16. Example 2 This template class is used in such a way:

  17. Example 3 If you want to use inheritors of singleton then the most flexible method is to use the registry of individuals.

  18. Example 3 Now information about that which exactly instance of singleton should we use, we can define the stage of the work program:

  19. Example 3 The main problem of using the registry of singletons consists in the registration of instances of singleton. For example, in C# for registration you use the static constructor.

  20. Example 3 In Java, with the same purpose you use a static block initialization

  21. Example 4 In C++ implementation of all singletons are variations of its main form

  22. Example 4 • All constructors are private, which does not allow the user code directly create objects of the class Singleton. • To protect from deleting the method “Instance” returns a reference but not a pointer. Also the class of destructor is declared as private. • Copy protection provides private copy constructor. • To protect from assignment (because the instance of object is theonly one and any assignment is a self-assignment) the assignment operator is declared private.

  23. Example 5. Meyers Singleton • Despite the fact that the memory for the singleton will be freed by the operating system, the object's destructor must be called in order to avoid resource leaks, which could the object request. • The first solution: singleton Meyers, which uses a local static variable:

  24. Example 6 • Meyers Singleton works in most cases, however, it does not solve the problem of hanging link that may occur in case of the reference to the object Singleton after its destruction. • The first solution of the problem of hanging links is to create flag “destroyed” and to throw an exception when you try to access to the destruction of the object

  25. Example 6

  26. Example 6. Phenix Singleton The second solution of the problem of hanging references consists in using the extended version of operator “new”, which allows you to create an object one again when you try to appeal the hanging link

  27. Function SetLongevity • The third solution of the problem of hanging links is to create an object with the explicit time of life. • The lifetime of the object can be set using the options SetLongevity of Loki library. • This function guarantees that object will be destroyed no earlier than objects with a lower time of life.

  28. Example 7. Setting time life for singleton • A sample implementation of a pattern Singleton with function SetLongevity:

  29. Example 8. Singleton in a multithreaded environment • To ensure the uniqueness of singleton in a multithreaded environment the double-checked locking is used. • Implementation of the function “Instance” is modified as follows:

  30. Example 8. Singleton in a multithreaded environment • In C#, a double-checked locking is implemented using the operator “lock”

More Related