Summary of 24 design patterns

I won’t explain what design patterns are here. You can read other articles on your own. Here I mainly collected articles that others have explained that are relatively easy to understand and summarized them, and then added my own understanding.

Before learning design patterns, you must first understand the seven principles of object-oriented

7 Principles of Object-Oriented DesignSOLID Dimit Synthesis Reuse

1.S: Classes, interfaces, and objects designed with single responsibility have only one single responsibility (not a single method, which can be understood as realizing a single function)

2.O: The open-closed principle  is closed to modifications and open to extensions.

(The opening and closing principle is idealistic and is the ultimate goal of object-oriented design. Other design principles can be regarded as means or methods to realize the opening and closing principle)

3.L: Liskov Substitution Principle:  Subclasses can extend the functions of the parent class, but cannot modify the functions of the parent class.

4.I: Interface isolation principle:  There should be no methods in an interface that are not used by the subclasses that implement it. Otherwise, the subclass that implements it will need to implement methods that the subclass should not implement.

5.D: Dependency Inversion Principle  High-level should not depend on low-level, both high-level and low-level should rely on abstraction. Abstractions should not depend on details, details should depend on abstractions.

6. Demeter's Law : The less you know, the better, that is, a class should maintain the minimum understanding of other objects.

7. Synthesis and reuse : Composition is better than inheritance. Don’t use inheritance where composition can be used.

Next let’s take a look at design patterns

Design pattern is a software design idea. It can not only be used in Unity, but also can be used as long as you need to design software. Design patterns can make code structure clearer

There are 24 design patterns, which can be divided into three categories: creative patterns, structural patterns, and behavioral patterns.

Creational patterns include :

1. The simple factory mode  encapsulates the New object, and the user can obtain the object by directly calling the encapsulation function.

2. The factory method pattern  solves the problem of modifying the factory code if you want to expand the product, that is, adding a layer of abstraction to the factory to implement the opening and closing principle.

3. Abstract factory adds the abstraction of multiple products, so that one factory can produce multiple products (because the factory method can only produce one product per factory)

4. Singleton mode allows only one instance of a class

5. Prototype mode does not use new when creating the same object, but copies it based on the existing object. C# provides the ICloneable interface, and deep copying can be achieved by implementing this interface (the difference between deep and shallow copies will not be described here)

6. The builder pattern factory only cares about getting the object. The builder cares about the object's initialization sequence and parameter combination input. The factory pattern cares about the overall (because it directly new a specific product without splitting the specific steps of building the product), and the builder pattern cares about the details (because it separates each step in building a product) You can find the difference by looking directly at the two comparison pictures. In the builder mode, the commander can be understood as the contractor, and the builder can be understood as the worker (there are senior, intermediate, and ordinary workers)

 Structural pattern :

7. Proxy mode  Class A needs to access the methods of class B. Through the proxy mode, the methods of class B are encapsulated into the proxy object, and some methods can also be added. Class A is directly called through the proxy object, thus protecting class B. And it can also facilitate the expansion of methods. The difference between the proxy mode and the adapter mode (the proxy object and the real object need to implement the proxy interface. It can be seen here that the proxy mode and the adapter mode are very similar in structure, but the former focuses more on enhancing functions based on the target method. , the latter focuses more on interface compatibility). Both proxy mode and command mode achieve their goals through middlemen, but there are differences . The decoration mode mainly focuses on enhancing the functionality of the object without changing the interface, while the proxy mode mainly focuses on adding some additional behavior or control during the object access process (proxy mode class A, class B both implement interface C; decorator mode It is class A, class B (decorator) implements interface C, and then class D inherits from class B). The difference between proxy mode and decorator mode The difference between  proxy mode and decorator mode

8. The core of the adapter pattern  is to reuse existing interfaces or methods, so that the two original interfaces (the interface here is not Interface, this is a general term and can be understood as a method for external use) do not match and cannot work together. Classes can work together. In short, the methods of class A cannot be used in class B. At this time, using the adapter pattern, the methods of class A converted by the adapter can be used in class B. (Both the adapter class and the adapter need to implement the target interface)

9. When applying various extension methods in the decorator mode  , inheritance is not used to generate subclasses. Instead, object association is used instead of inheritance, which enhances the functionality of the class.

10. The bridge mode  separates objects and behaviors to facilitate multi-dimensional combination of objects. The bridge mode is a specific implementation of the synthesis and reuse rules (the biggest difference between the strategy mode and the bridge mode is that the former is to encapsulate a series of algorithms so that the algorithms can be replaced Use, the purpose of the latter is to separate things and abstractions, and combine objects in multiple dimensions)

11. Combination model School, college, department If you use the traditional method, you need to regard the college as a subcategory of the school, and the department as a subcategory of the college. The combination model eliminates the inheritance relationship, but treats it as a tree structure, with nodes It is formed in the form, which facilitates the addition, deletion, traversal, etc. of schools, colleges, and departments.

12. Flyweight mode The core of flyweight mode is the object pool. When using an object, first obtain the object from the object pool . If there is no object pool, create one, put it into the object pool, and then obtain it from the object pool ; (This The object pool is different from the object pool in the traditional sense. The objects in the object pool of the flyweight mode can be shared by multiple modules. For example, both module one and module two use object A, so you can use the flyweight mode. Put object A in the object pool for use by multiple modules; in the traditional sense, there are multiple identical objects in an object pool. When an object is used by one module, it cannot be used by another module. In one sentence, an object in the flyweight model can be used by multiple modules, while in the traditional sense, an object in the object pool can only be used by one module)

13. Appearance mode  adds a layer of appearance layer (non-abstract), encapsulates many subsystems into this layer, and this layer only provides an interface to the outside. It can be said that appearance mode is the practical application of Demeter's law.

Behavioral pattern:

14. Observer Pattern  The Observer Pattern defines a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated. It is mainly used to decouple code. The specific implementation of the observer pattern is event monitoring and broadcasting in Unity

15. The process steps of the template method model algorithm  are fixed, but the template method can be used when the calculation content is different (for example, if you want to go to the bank to handle some business, the process you must go through is to take the number, queue, handle the business, and leave. Now in addition to handling the business Except for the differences, the rest of the steps are the same. At this time we can use the template method pattern to design)

16.  The command mode   adds a specific implementation object of command. The command object needs to specify a specific executor. The execution method (execute) of the command object is an encapsulation of some methods of the executor. The caller can hold the command object or the command object. Collection, the execution method of the caller is to execute the execute method of the held command object. In this way, you only need to create different command objects (specify different executor methods), assign the command object to the caller, and then the caller can call Different executor methods, UML diagram reference link

17.  State mode The specific implementation of state mode is the FSM finite state machine in Unity

18. Responsibility chain mode The processing of a request requires the cooperation of one or several objects among multiple objects for processing; using the idea of ​​a linked list, each object is a node, and the object contains the next node.

19. The interpreter mode  is an interpreter created for the language in order to interpret a language;

20. The intermediary mode  is used for mutual communication between multiple objects. Adding an intermediary role turns the original network structure into a star structure (observer mode: notifications can only be circulated from one party, which is one-way. ( Just like in class, you can only teach honestly to all the students in the class, not the other way around.) Mediator model: notifications can be circulated from any party, and it is two-way. (Like a sharing meeting, everyone can share their own things with others)

21. The visitor pattern requires many different operations on the objects in an object structure (these operations are not related to each other). At the same time, it is necessary to avoid letting these operations "pollute" the classes of these objects. This means that there is a series of objects with different roles. To obtain the data in the object, different roles have different concerns. Role 1 wants to obtain the data A of the object, and role 2 wants to obtain the data B of this series of objects, so they can use the visitor pattern.

22. The biggest feature of the strategy pattern  is that behaviors can be replaced with each other, so the algorithm can be used interchangeably.

23. The memo mode  is used to record the internal state of an object. The memo object serves as an intermediate object. The object that needs to record the state can create a memo object to record the current state. The manager uses the memo stack to store multiple memo objects. After creating a memo of the new state of an object, it can be put into the memo stack to facilitate subsequent resumption of reading data.

24. The generator pattern allows you to traverse all elements in a collection without exposing the underlying representation of the collection (list, stack, tree, etc.). Code reference

Guess you like

Origin blog.csdn.net/qq_62947569/article/details/135045265