What are design patterns?
Christopher Alexander said, “Each pattern describes a recurring problem around us and the core of the solution to that problem. That way, you can use the solution again and again without having to reinvent the wheel.” To put it simply:
Design Pattern is a set of repeatedly used, most people know, classified, summarized code Design experience, the use of Design Pattern in order to reuse the code, make the code easier to understand and ensure the reliability of the code.
Why are there design patterns
Design the standard of pattern development, the optimal scheme, choose the right pattern can greatly improve the efficiency of development, less detours. If the design pattern is compared to “thirty-six plans”, then every pattern is a strategy, it is born to solve a certain kind of problems, design pattern can solve some problems well.
Classification of design patterns
Design patterns common in classic application frameworks fall into three categories:
- Creative pattern: Abstraction of the instantiation process of a class. When creating objects, some systems need to dynamically decide how to create objects, which objects to create, and how to combine and represent them. The creation pattern describes how these dynamic decisions are constructed and encapsulated. Contains a class creation mode and an object creation mode.
Common creation patterns are: Factory Factory pattern; Singleton Singleton pattern; Prototype
- Structural pattern: Describes how classes or objects are joined together to form a larger structure. It is divided into class structure pattern and object structure pattern. The class structure pattern uses inheritance to group classes, interfaces, and so on together to form a larger structure. The structural pattern of a class is static. The object structure pattern describes how objects of different types are grouped together to implement new functions. The structural pattern of an object is dynamic.
Common structural patterns are: Adapter Adapter pattern; Decorator Decorator pattern; Proxy mode
- Behavioral patterns: Abstractions that divide responsibilities and algorithms between different objects. Not just about classes and objects, but about how they interact with each other. The behavior pattern of a class uses inheritance relationships to distribute behavior among several classes. The behavior pattern of objects uses aggregation of objects to assign behavior.
The common behavioral patterns are: Strategy; Template Template mode; Delegate Mode; Delegate mode. Observer mode
Design pattern frequency ranking
Note: There is no Delegate pattern in the 23 commonly used design patterns, but Spring does use a Delegate pattern. The DispatcherServlet in Spring MVC framework actually uses the Delegate pattern.
Singleton
Singleton pattern: The Singleton is used to ensure that only one instance of a Class exists in the application. And provide global access. Singleton limits the number of instances to facilitate GC collection.
Strategy Pattern
Policy pattern: The policy pattern takes a set of algorithms and encapsulates each algorithm into a separate class with a common interface, making them interchangeable. The policy pattern allows the algorithm to change without affecting the client. Strategic patterns separate behavior from context. The environment class is responsible for maintaining and querying the behavior class, and the various algorithms are provided in the specific policy class. Since the algorithm and the environment are independent, the increase, decrease and modification of the algorithm do not affect the environment and the client.
Prototype mode
Stereotype pattern: Give a stereotype object to indicate the type of object to be created, and then create more objects of the same type by copying this stereotype object. The original model pattern allows you to dynamically add or subtract product classes. Product classes do not have to have any predefined hierarchy. The original model pattern applies to any hierarchy. The downside is that each class must be equipped with a clone method
Because Java provides the clone() method to clone objects, implementing the Prototype pattern is suddenly easy.
Factory Mode
Factory pattern: Defines an interface for creating objects and lets interface subclasses decide which class to instantiate using factory methods.
Decorator pattern
Decorator pattern: Decorator pattern extends the functionality of objects in a transparent way to the client and is an alternative to inheritance, providing more flexibility than inheritance. Adding functionality to an object dynamically can be undone dynamically. Add a very large number of functions from permutations and combinations of basic functions.
The reason for using decorators is that these functions need to be dynamically decided by the user how and when to add them. Decorators provide a “plug and play” approach to deciding at run time what functionality to add and when.
Adapter mode (Adapter)
Adapter pattern: Transforms the interface of one class into another interface that the client expects, thus enabling two classes to work together that would otherwise not work together due to interface mismatches. An adaptive class can return an appropriate instance to the client according to the parameters. Two incompatible classes are used together. It belongs to the structural mode and requires two identities of Adaptee and Adaptor.
Why? The first solution to combining two unrelated classes is to modify their interfaces if we don’t have the source code, or if we don’t want to modify their interfaces for an application. How to do? Using Adapter, create a hybrid interface (hybrid) between the two.
How to use it? There are two ways to implement Adapter, as mentioned in the “Class regeneration” section of “Think in Java” : Composition and inheritance.
Proxy mode
Proxy mode: The proxy mode provides a proxy object to an object and controls references to source objects. Agency is when a person or agency acts on behalf of another person or agency.
In cases where a client does not want or is unable to reference an object directly, a proxy object can act as a direct intermediary between the client and the target object. The client cannot distinguish the proxy subject object from the real subject object. The proxy mode may not know the real proxied object, but only has an interface of the proxied object. In this case, the proxy object cannot create the proxied object, and the proxied object must be created and passed in by other roles in the system.
Observer Model
Observer pattern: The observer pattern defines a multi-group dependency that allows multiple observer objects to listen on a subject object at the same time. When the topic object changes in state, all observer objects are notified so that they can update themselves automatically. Publish and subscribe.
Template pattern (Template)
Template pattern: The template method pattern prepares an abstract class that implements some of the logic as concrete methods and concrete constructor children, and then declares some abstract methods that force the subclasses to implement the rest of the logic.
Different subclasses can implement these abstract methods in different ways, resulting in different implementations of the remaining logic. Start with a top-level logical framework and leave the details of the logic to specific subclasses.
A quick summary of design patterns
That’s the thing about design. If you think about it, you can solve the problem elegantly. If you don’t think about it, you can only use the method of modifying the code everywhere.
Now I’m glad I learned design patterns instead of taking someone else’s “advice” : We didn’t use design patterns in our projects, and it didn’t work. Design mode is a good thing, and I will definitely study it further in the future, and practice more in the project to improve my design ability.
Of course, I also recommend you to watch this video on design patterns in group 895244712 for some inspiration. In fact, the design pattern is not difficult, difficult is really understand its subtle, and can be flexibly applied to the development of daily projects.