Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem...

30
Design patterns

Transcript of Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem...

Page 1: 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.

Design patterns

Page 2: 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.

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.

Page 3: 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.

3

Reasons to use and classification• Reusability• Use of another's experience instead of self-invention• Unite terminology• The level of abstractionCreational 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 encapsulationalgorithms and the distribution of responsibilities between objects. There are also concurrency patterns, system patterns, integral patterns etc.

Page 4: 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.

4

Class image

Public method

Protected method

Private method

Page 5: 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.

5

Association

Customer has a lot of accounts:

Class of application uses class of connection:

Page 6: 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.

6

Generalization

Class Circle is an inheritor of a class Shape:

Page 7: 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.

7

AggregationClass 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):

Page 8: 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.

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

Page 9: 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.

9

Pattern Singleton

Page 10: 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.

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.

Page 11: 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.

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

Page 12: 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.

12

Class diagram

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

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.

Page 14: 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.

14

Example 1Standard implementation:

Page 15: 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.

15

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

Page 16: 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.

16

Example 2

This template class is used in such a way:

Page 17: 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.

17

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

Page 18: 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.

18

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

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

19

Example 3The 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.

Page 20: 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.

20

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

Page 21: 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.

21

Example 4

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

Page 22: 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.

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.

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

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:

Page 24: 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.

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

Page 25: 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.

25

Example 6

Page 26: 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.

26

Example 6. Phenix SingletonThe 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

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

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.

Page 28: 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.

28

Example 7. Setting time life for singleton

• A sample implementation of a pattern Singleton with function SetLongevity:

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

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:

Page 30: 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.

30

Example 8. Singleton in a multithreaded environment

• In C#, a double-checked locking is implemented using the operator “lock”