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 # exampleIn 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 # exampleIn Russian with picturesSingleton - 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 # exampleIn Russian with pictures2. 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 # exampleIn Russian with picturesComposite - 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 picturesDecorator 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 # exampleIn Russian with picturesProxy - 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 # exampleIn Russian with pictures3. 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 # exampleIn Russian with picturesIterator - 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 # exampleIn German with picturesMemento - 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 # exampleOn Ukrainian with picturesObserver - 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 # exampleIn Russian with picturesMediator 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 # exampleOn Ukrainian with picturesState - 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 # exampleOn Ukrainian with picturesStrategy - 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 # exampleIn Russian with picturesVisitor - 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 # exampleIn Italian with picturesupd: Technical editor - Anton.