A brief introduction to design patterns

The so-called design pattern is a set of repeatedly used, categorized and catalogued, and a summary of code design experience. The use of design patterns is to reusable code, make the code easier to understand by others, and ensure the reliability of the code. Design patterns make coding truly engineered.

Pattern: A scheme to solve a problem in a certain environment, including three basic elements - problem, solution and environment (fixed routine).

 

Classification of Design Patterns

creative mode

  • Usually related to the creation of objects, involving the way objects are instantiated. (5 modes in total)

1. The purpose of the Factory Method Pattern is to define a factory interface for creating product objects and defer the actual creation to subclasses.
2. The intent of the Abstract Factory Pattern is to provide a set of related or interdependent interfaces without specifying their specific classes.
3. The intent of the Builder Pattern is to separate a complex build from its representation, so that the same build process can create different representations.
4. Prototype Pattern (Prototype Pattern) is to use prototype instances to specify the types of objects to be created, and to create new objects by copying these prototypes.
5. The Singleton Pattern ensures that a class has only one instance and provides a global access point to access it.

Structural Pattern

  • Describes how to combine classes and objects for larger structures. (7 modes in total) 

6. Proxy Pattern is to provide a proxy for other objects to control access to this object.
7. The Decorator Pattern dynamically adds some additional responsibilities to an object. This pattern is more flexible than subclassing in terms of adding functionality. 
8. The Adapter Pattern is to convert the interface of a class into another interface that the client wants. Makes classes that would otherwise not work together due to interface incompatibility work together. 
 9, Bridge Pattern (Bridge Pattern) is to separate the abstract part from the actual part, so that they can be changed independently.
10. The Composite Pattern is to combine objects into a tree structure to represent a "part-whole" hierarchy. Make users consistent in the use of single objects and composite objects.
11. The Facade Pattern is to provide a consistent interface for a set of interfaces in the subsystem. This pattern defines a high-level interface that makes the subsystem easier to use.
12. Flyweight Pattern (Flyweight Pattern) efficiently supports a large number of fine-grained objects in a shared way 

behavioral model

  • Used to describe how classes or objects interact and how responsibilities are assigned. (11 modes in total) 

13. The Template Method Pattern allows subclasses to redefine certain steps of an algorithm without changing the structure of the algorithm. 
14. The Command Pattern encapsulates a request as an object, allowing you to parameterize the client with different requests; queue or log requests, and support undoable operations.
15. Chain of Responsibility Pattern. In this pattern, many objects are connected to form a chain by each object's reference to its subordinate. Requests are passed on this chain until some object on the chain decides to handle the request, which allows the system to dynamically reorganize the chain and assign responsibilities without affecting clients. 
 16. The Strategy Pattern is to prepare a set of algorithms and encapsulate each algorithm so that they are interchangeable.
17. The Mediator Pattern is to define a mediator object to encapsulate the interaction between a series of objects. Terminators make each object unnecessarily call each other, so that it is loosely coupled, and can independently change the interaction between them.
18. The Observer Pattern defines a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.
19. The Memento Pattern captures the internal state of an object and saves this state outside the object without destroying the encapsulation.
20. The Visitor Pattern (Visitor Pattern) is an operation that acts on each element in an object structure, which allows you to define new operations that act on these elements without changing the class of each element.
21. The State Pattern is the behavior of an object that depends on the state it is in. 

 

ps:

Difference between factory pattern and builder pattern

In Factory mode:
1. There is an abstract factory.
2. Realize a specific factory---automobile factory.
3. The factory produces automobile A and obtains automobile product A.
4. The factory produces automobile B and obtains automobile product B.  
In doing so, the isolation of buyers and production lines is achieved. The emphasis is on results.      
Builder mode:
1. The engine factory produces engine products and gets auto parts A.
2. The tire factory produces wheel products and obtains auto parts B.
3. The chassis factory produces body products and obtains auto parts C.
4. Put these parts together to form a whole that can be assembled into a car.
5. Send the whole to the car assembly factory to get a car product.
This is done in order to achieve decoupling of complex object lines and their components. The emphasis is on the process
. The difference between the two is: The
Factory pattern does not consider the assembly process of the object, but directly generates an object I want.
The Builder mode first creates each part of the object one by one, and then assembles it into an object.
The problem that the Factory pattern solves is that factories produce products.
The problem solved by the Builder pattern is that the factory controls the process of assembling the various components of the product generator, and then obtains the product from the product generator.
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324357776&siteId=291194637