📜 ⬆️ ⬇️

Design patterns for humans.

There was a task as soon as possible (it was necessary to pass the test) to a person far from programming to explain patterns at the simplest level.
As a result of the balance of simplicity of expression and adequacy, this crib was formed.
Please rate this very value and simplicity.

Likbez:
Encapsulation - hiding the essence of the object behind the interface (we like the picture, we only have to be able to display, resize, rotate ...)
An instance of a class is an object :)

Brief introduction:
Patterns are divided into 3 main groups:
1. Generators - patterns responsible for the creation of objects;
2. Structural - determine the structure of the presentation of classes / objects;
3. Patterns of behavior - patterns for encapsulating (hiding) actions on objects.

1. Generators


Abstract Factory - creates objects of similar classes.
We have an abstract class and heirs, we are creating an abstract factory and concrete factories for heirs. It is necessary to separate the method of creating objects of different classes. Classes must be related.
C # example
In Russian with pictures
')
Builder - separates the creation of the object and its presentation.
Allows you to get different results when calling the same creation method.
C # example
In Russian with pictures

Singleton - guarantees the presence of a single instance of the class.
It is used if the system must be guaranteed to have one instance of a class (object). For example, in the system there can be only one accounting form for black bookkeeping. This is an analogue of a global variable, but much cooler.
C # example
In Russian with pictures

2. Structural


Adapter - adapts :) the interface of one class to another.
Suppose there are two classes, and we have access only to interfaces (code to change nizya). And you need to establish interaction. Then the pattern is applied. It takes a class A interface and its methods transforms for class B.
Example: there is a class for working with complex numbers, it has methods ToCompl (int i) and there is another class that knows about complex numbers but thinks that the method there is called ConvertToComplex (int i), we are doing the adapter for the first class, which will have the method ConvertToComplex , and in it to call ToCompl (stupid example, it operates only with names, but in real life there you can replace everything, for example, to call not 1 method, but 15)
C # example
In Russian with pictures

Composite - allows you to create one simple interface for a heap of similar classes.
Suppose there are different geometric shapes (Line, Circle), we inherit them from the class Figure, which has a method Draw, and thus we will provide a simple draw call for all heirs.
C # example
In Russian with pictures

Decorator Dynamic addition of functionality for the class.
For example, there is a photo class, and we should hang a frame, we will make a class that will display a photo (Photo class method) and then display a frame (using our own methods)
C # example
In Russian with pictures

Proxy - replaces the class itself :)
It turns out that for the entire proxy system looks like a necessary class, but it is not.
For example, there is a large object (pdf document) and we need to display it on the screen. It is impossible to load the whole (it will not fit into the memory). Therefore, we make a proxy that will be loaded and output page by page, but it will look like this document.
C # example
In Russian with pictures

3. Patterns of behavior


Command - encapsulates the action in the object.
For example, there is a menu that should display a message. We do not just write the output in the menu handler, but do the class that does it. The tree of such classes allows you to organize such a thing as Undo / Fluorite, in fact, we can save our actions.
C # example
In Russian with pictures

Iterator - allows you to bypass the collection of objects. For example, there is a vector (array) and instead of [] we write the methods Next, Previos. Thus, we are not tied to an array and in the future we can replace it with another structure.
C # example
In German with pictures

Memento - allows you to save the state of the object. For example, in Photoshop, we have the cancellation of commands, we used an atsky filter which fucked up the whole photo, there is no reverse function of this filter (the command doesn’t skip the pattern), we save the photo before applying the filter, and then we can restore it.
C # example
On Ukrainian with pictures

Observer - improves the interaction of objects. For example, there is a mold, we press a button and in the text field “42” is displayed, you can write stupidly in the handler of the type button to change the field, but this is not true. Tru: we create the Observer object (usually it is singelton) and in the button we call the method in this Observer, and he already passes this message to all the text fields (there may be more of them :)
C # example
In Russian with pictures

Mediator is the same as the Observer, but there is a lot-to-many attitude.
For example, we have not only a button, but also a menu.
C # example
On Ukrainian with pictures

State - allows you to replace the class of his heirs.
For example, there is an application, it is either processed or unprocessed (no third is given). We make classes for these two states and inherit from the Abstract State Application State class, which has the ChangeState method. In the implementation of this method, we will replace the current state with the object we need (Processed, Untreated) these states are usually singiltons.
C # example
On Ukrainian with pictures

Strategy - allows you to encapsulate algorithms.
For example, we need to sort the array and we don't care what method (bubble, fast ...) so we create a class, and from it the heirs - specific algorithms, and already there - the Sort methods, while in the concrete sorting methods we implement them. This allows other classes to not know how and what we sort.
C # example
In Russian with pictures

Visitor - allows you to hang a functional on an already finished class.
For example, there is a worker, he has a black and white salary. It is considered in different ways. Employee class change nizya. We make a visitor who will have two methods Calculate Really and Calculate For Tax Officers. He will take the Worker object, pull a bet out of it and count it differently.
C # example
In Italian with pictures

upd: Technical editor - Anton.

Source: https://habr.com/ru/post/49365/


All Articles