23 design patterns:
Ii. 23 design modes:
Definition/Concept | ||
---|---|---|
1 | Simple Factory Pattern Simple Factory Pattern | In terms of the type of design patterns, the simple factory pattern belongs to the creation pattern, also known as the StaticFactory Method pattern, but it is not one of the 23 GOF design patterns. The simple factory pattern isA factory object decides which instance of a product class to create. Simple factory pattern is the most simple and practical pattern in the factory pattern family, which can be understood as a special implementation of different factory patterns. |
2 | Strategy Pattern Strategy Pattern | The ## Strategy pattern defines a series of algorithms, encapsulates each algorithm, and makes them interchangeable. The policy pattern lets the algorithm change independently of the customers that use it.,Abstract Policy roles: Policy classes, usually implemented by an interface or abstract class. ## 2) Concrete policy roles: wrap related algorithms and behaviors. ## 3) Context role: Holds a reference to a policy class that is ultimately called by the client. |
3 | Decorator Pattern | Adding additional responsibilities to an object dynamically gives decorator patterns more flexibility than subclassing in terms of adding functionality. Conclusion: To remove the decoration function from the class, this can simplify the existing class; Biggest benefit: Effectively separating the core responsibility of the class from the decoration function. It also removes repetitive decorative logic. |
4 | Proxy Pattern Proxy Pattern | Provide a proxy for other objects to control access to this object. In some cases, an object may not want or be able to refer directly to another object, whereas a proxy object can act as an intermediary between the client and the target object |
5. | Factory Method mode Fatory Method | Define an interface for creating objects and let subclasses decide which class to instantiate. The factory method delays the instantiation of a class to its subclasses. 【 achieve 】 the factory method pattern to realize the client needs to decide which one to instantiate the factory class to implement the operation, choose to determine there is a problem, that is to say, the factory method to simple factory internal logic to transferred to the client code, you want to add functionality, was to change the factory class, now is to modify the client. |
6 | Prototype Pattern | Specify what kind of objects to create with prototype instances, and create new objects by copying these prototypes. A prototype object is simply a matter of creating another customizable object from one object without knowing any of the details of the creation. |
7 | Template Method mode | Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. The template approach allows subclasses to redefine specific steps of an algorithm without changing the structure of that algorithm. The template method pattern works to its advantage by moving immutable behavior to the superclass, removing duplicate code from subclasses. |
8 | Facade Pattern | To provide a consistent interface for a set of interfaces in a subsystem, this pattern defines a high-level interface that makes the subsystem easier to use. |
9 | Builder Pattern | Separating the construction of a complex object from its representation allows the same construction process to create different representations. [Note:] First it is intended to build a complex object, which often requires several objectsChild objectsOr step by step to complete the final construction of this object, and the child object of this complex object often needs to change, but its construction process is relatively stable. Essence: to construct the same object through a unified process or constraint. |
10 | Observer mode Publish-subscribe mode publish-subscribe Pattern | Defines a one-to-many dependency that lets multiple observer objects listen on a subject object at the same time. The topic object notifies all observer objects when their state changes, enabling them to update themselves automatically. |
11 | Abstract Factory Pattern | Provides an interface to create a series of related or interdependent objects without specifying their specific classes. 【 Purpose 】 : For exchange product series, such as ACCESS->SQL SERVER; The concrete class name of the product is separated by the implementation of the concrete factory. |
12 | Status Mode State Pattern | Allowing an object to change its behavior when its internal state changes, the object appears to have changed its class. 【 Main solution 】 : object state transformation of the conditional expression is too complex. Complex decision logic can be simplified by moving the decision logic of states into a class that represents different states. |
13 | Adapter Pattern Adapter Pattern | Translate the interface of a class into another interface that the customer wants. The Adapter pattern enables classes to work together that would otherwise not work together due to interface incompatibilities. |
14 | Memento Pattern Memento Pattern | Capture the internal state of an object and store the state outside of the object without breaking encapsulation. This allows the object to be restored to its original saved state later. |
15 | Composite Pattern Indicates the Composite Pattern | Group objects into a tree structure to represent a partial-whole hierarchy. The composite pattern makes the use of single objects and composite objects consistent. [When to use] : The composite pattern should be considered when you find that the requirement is a structure that reflects the hierarchy of parts and whole, and you want users to be able to use all objects in the composite structure uniformly, regardless of the differences between the composite objects and individual objects. |
16 | Singleton Pattern | Ensure that a class has only one instance and provide a global access point to access it. Because the Singleton class encapsulates its unique instance, it has strict control over how and when customers access it. In simple terms, controlled access to a unique instance. |
17 | Bridge mode Bridge Pattern | Separate the abstract part from its implementation part so that they can all change independently. 【 explanation 】 : The implementation system may have multiple classifications, each of which is likely to change, so separate this multiple classifications and let them change independently, reducing the coupling between them. |
18 | Command Pattern Command Pattern | Encapsulate a request as an object, allowing you to parameterize clients with different requests. Queue or log requests, and support undoable operations. |
19 | Chain of Responsibility Pattern | This avoids coupling between the sender and receiver of the request by giving multiple objects the opportunity to process the request. The object is concatenated into a chain and the request is passed along the chain until an object processes it. Key: When a client submits a request, the request is passed down the chain until a ConcreteHandler object handles it. |
20 | Mediator Pattern | A mediation object encapsulates a set of object interactions. The mediator loosens the coupling by eliminating the need for objects to explicitly refer to each other, and can change their interactions independently. [Application scenario] : Where a set of objects are well defined but need to communicate in a complex way, and where you want to customize a behavior distributed across multiple classes without generating too many subclasses. |
21 | Enjoy the FlyWeight Pattern | This technique effectively supports a large number of granular objects. [Usage] : can avoid a lot of similar class overhead. For C++, this means sharing a block of memory. Object Pointers point to the same place. If an application uses a large number of objects and these objects cause significant storage overhead, it should be considered. Also, most of the object’s state can be external state. If you remove the external state of the object, you can replace multiple groups of objects with fewer shared objects, and then you can consider using shared elements. |
22 | The Interpreter Pattern | Given a language, define a representation of its grammar, and define an interpreter that uses that representation to interpret sentences in the language. 【 用】 : If a particular problem occurs frequently enough, it may be worthwhile to express instances of the problem as sentences in a simple language. This allows you to build an interpreter that solves the problem by interpreting the sentences. |
23 | Visitor Pattern Visitor Pattern | Represents an operation that operates on elements in an object structure, allowing you to define new operations on those elements without changing their classes. Advantages: Adding a new action is easy because adding a new action means adding a new visitor. The visitor pattern aggregates the related behavior in a visitor object. Disadvantages: Adding new data structures becomes difficult. |
___ Most of the content is taken from The Big Talk Design Pattern
Personal feelings:
1. Maybe when you first look at each design pattern, you can see how it works through examples? But when there are multiple patterns together, such as adapter pattern, bridge pattern, mediator pattern difference? It may not be clear enough (of course, it also means that you do not fully understand, on the other hand, it means that you do not use relevant design patterns.
2. Only by applying the design pattern in practice can we truly perceive its beauty, which is also what I lack.
\