Talking about common design patterns in java,


Preface

If a worker wants to do his job well, he must first sharpen his tools.
The design pattern is not a silver bullet, but it can solve most problems at the lowest cost in the project. So a full understanding of design patterns is very practical in daily development. Design patterns are reusable solutions abstracted by people for the same problem in software development. To some extent, design patterns have represented best practices for specific situations.
Design Pattern is a summary of the predecessors' experience in code development, and a series of routines to solve specific problems. It is not a grammatical regulation, but a set of solutions to improve code reusability, maintainability, readability, robustness, and security.

In 1995, GoF (Gang of Four, Gang of Four) co-published the book "Design Patterns: The Foundation of Reusable Object-Oriented Software", which included a total of 23 design patterns, and since then established the software design pattern field Milestone, known as "GoF Design Mode"


Tip: The following is the content of this article, the following cases are for reference

1. GOF23 design patterns

Creation mode is a summary of various problems and solutions in the process of object creation. (5 types)

Singleton mode: A certain class can only generate one instance. This class provides a global access point for external access to the instance, and its expansion is a limited multiple instance mode.
Prototype mode: Use an object as a prototype, and clone multiple new instances similar to the prototype by copying it.
Factory Method (Factory Method) mode: Define an interface for creating products, and subclasses decide what products to produce.
Abstract Factory (AbstractFactory) mode: Provides an interface to create a product family, each of which can produce a series of related products.
Builder mode: Decompose a complex object into multiple relatively simple parts, then create them separately according to different needs, and finally construct the complex object.



The structural model is a summary of the software design structure, focusing on the practical experience of class, object inheritance, and combination methods. (7 types)

Proxy mode: Provide a proxy for an object to control access to the object. That is, the client indirectly accesses the object through the proxy, thereby restricting, enhancing or modifying some of the characteristics of the object.
Adapter mode: Convert the interface of a class to another interface that the customer wants, so that the classes that cannot work together because of the incompatibility of the interface can work together.
Bridge mode: Separate abstraction from implementation so that they can be changed independently. It is realized by replacing inheritance relations with composition relations, thereby reducing the coupling degree of the two variable dimensions of abstraction and realization.
Decoration (Decorator) mode: dynamically add some responsibilities to the object, that is, increase its additional functions.
Facade mode: Provides a consistent interface for multiple complex subsystems, making these subsystems easier to access.
Flyweight mode: Use sharing technology to effectively support the reuse of a large number of fine-grained objects.
Composite mode: Combine objects into a tree-like hierarchy, so that users have consistent access to single objects and composite objects.




Behavioral mode is a mode summarized from the perspective of interaction between classes or objects and division of responsibilities. (11 types)

TemplateMethod (TemplateMethod) mode: Define the algorithm skeleton of an operation, and delay some steps of the algorithm to the subclass, so that the subclass can redefine certain specific steps of the algorithm without changing the structure of the algorithm.
Strategy mode: defines a series of algorithms, and encapsulates each algorithm so that they can be replaced with each other, and the change of the algorithm will not affect the customers who use the algorithm.
Command mode: Encapsulate a request as an object to separate the responsibility of issuing the request from the responsibility of executing the request.
Chain of Responsibility (Chain of Responsibility) mode: Pass the request from one object to the next in the chain until the request is responded. In this way, the coupling between objects is removed.
State mode: Allows an object to change its behavior ability when its internal state changes.
Observer mode: There is a one-to-many relationship between multiple objects. When one object changes, the change is notified to multiple other objects, thereby affecting the behavior of other objects.
Mediator mode: Define an intermediary object to simplify the interaction between the original objects, reduce the coupling between the objects in the system, so that the original objects do not have to understand each other.
Iterator mode: Provides a way to sequentially access a series of data in the aggregate object without exposing the internal representation of the aggregate object.
Visitor mode: Provide multiple access methods for each element in a collection without changing the collection elements, that is, each element has multiple visitor objects to access.
Memento (Memento) mode: Under the premise of not destroying the encapsulation, obtain and save the internal state of an object in order to restore it later.
Interpreter mode: Provides how to define the grammar of the language and the method of interpreting language sentences, namely the interpreter.

Two, expansion

How Spring, etc. use design patterns in API design. Such as:

  • BeanFactory and ApplicationContext apply the factory pattern.

  • In the creation of Beans, Spring also provides singleton and prototype implementations for objects defined in different scopes.

  • In the field of AOP, proxy mode, decorator mode, and adapter mode are used.

  • Various event listeners are typical applications of the observer pattern.

  • Similar to JdbcTemplate, etc., the template mode is applied.

to sum up

1 It is very important to analyze the design pattern of good field application.

I will launch a corresponding topic in the later period. Please students who have watched move your little hand, leave more comments, and more likes. Your comments and likes are my motivation to stick to it, and I hope you have high-quality communication! ! !

Guess you like

Origin blog.csdn.net/aa327056812/article/details/109600552