Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.

Similar presentations


Presentation on theme: "Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as."— Presentation transcript:

1 Design patterns

2 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». 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 converted in code, rather it is a description or a sample for how to solve the problem so that it can be used in various situations. Design pattern is a description of the interaction of objects and classes, 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. 2

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 patterns are responsible for the encapsulation algorithms and the distribution of responsibilities between objects. There are also concurrency patterns, system patterns, integral patterns etc. 3

4 Class image 4 Public method Protected method Private method

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

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

7 Aggregation 7 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 and the 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 8

9 Pattern Singleton 9

10 Definition and motivation Singleton is a generating pattern, which guarantees that a class has only one instance, 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. 10

11 Advantages Controlled access to the single instance. As class “Singleton” encapsulates its only instance, 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). 11

12 Class diagram 12

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. 13

14 Example 1 Standard implementation: 14

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

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

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

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

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. 19

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

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

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 the only one and any assignment is a self-assignment) the assignment operator is declared private. 22

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: 23

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 24

25 Example 6 25

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 26

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. 27

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

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: 29

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


Download ppt "Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as."

Similar presentations


Ads by Google