It took two months! With 20+ real case scenarios, nearly 300 pages, and 18W words, this [Java Design Pattern Practice Guide] has become a god and can be called a classic

What is a design pattern? I have asked this question no less than dozens of interviewers, and the answers are varied.

In my opinion, the pattern is the experience, and the design pattern is the design experience. With this experience, we can use specific designs and combination designs in specific situations, which can greatly save our design time and improve work efficiency.

As an old code farmer who has been working for a long time, I have experienced a lot of system architecture design. Next, I will summarize some of the architecture design patterns used in my work into a document to share with you, I hope you will not go detour.

Creator mode (five types)

  • Factory method pattern

The factory pattern, also known as the factory method pattern, is a creational design pattern that provides a method for creating objects in the parent class, allowing the subclass to determine the type of object to be instantiated.

  • Abstract factory pattern

Although the main intention of the abstract factory pattern and the factory method pattern is to solve the problem of interface selection. But in terms of implementation, the abstract factory is a central factory, creating a pattern for other factories.

  • Builder mode

What the builder mode accomplishes is the process of constructing a complex object by assembling multiple simple objects step by step.

  • Prototype mode

The main problem that the prototype mode solves is to create duplicate objects, and the content of this part of the object itself is more complicated. The generation process may take a long time to obtain data from the library or RPC interface, so the cloning method saves time.

  • Singleton mode

The singleton pattern can be said to be one of the simplest patterns in the entire design, and this method is often used in coding development even without looking at the design pattern related information.

Structural mode (seven types)

  • Adapter mode

The main function of the adapter mode is to unify the originally incompatible interfaces through adaptation and modification. To make it easy for users to use, like the universal charger, data cable, MAC notebook adapter, and a socket for traveling abroad, they are all designed to adapt to various interfaces and make them compatible. .

  • Bridge mode

The main function of the bridge mode is to combine a variety of matching uses by separating the abstract part from the implementation part. To put it bluntly, the core implementation is to include the B interface in the A class, and pass the realization of the B class through the constructor. This B class is the bridge of the design.

  • Combination mode

As you can see from the picture above, this is a bit like a screw and a nut. A structure tree is organized through a bunch of links. The design idea of ​​combining similar objects (also known as methods) into a set of structure tree objects that can be called is called combination mode.

  • Decorator mode

At first glance, the picture above feels that the decorator mode is a bit like a Russian doll or a certain car, and the core of the decorator is to add new functions to the class without changing the original class. Without changing the original class, some friends may think of inheritance and AOP aspects. Of course, these methods can be implemented, but using the decorator pattern is another way of thinking that is more flexible and can avoid too many subclasses caused by inheritance. It can also avoid the complexity caused by AOP.

  • Appearance mode (facade mode)

The appearance mode is also called the facade mode. The main solution is to reduce the complex logic combination of the caller's use of the interface. In this way, the caller and the actual interface provider provide an intermediate layer for packaging logic to provide API interfaces. Sometimes the appearance pattern is also used in middleware to package the common and complex logic in the service at the middleware layer, so that the user can only care about business development.

  • Flyweight model

Flyweight mode is mainly to share common objects, reduce memory usage, and improve system access efficiency. And this part of the shared objects usually consumes more memory or needs to query a large number of interfaces or use database resources, so they are unified and used as shared objects.

  • Agency model

The agency model is a bit like the boss and younger brother, and also a bit like a distributor. The main solution is to provide easy-to-use proxy services for access to certain resources and easy-to-use operations of object classes. And this pattern of design thinking often appears in our system, or in the components you have used, they all provide you with a very simple and easy-to-use way to control the services that you originally need to write a lot of code to use. class.

Behavior mode (ten types)

  • Chain of Responsibility Model

The core of the chain of responsibility model is to solve the sequential execution and processing relationship in a group of services. It's a bit like you have no money to spend and need family financial expenditure approval. If you are under 10 yuan, you need to look for your girl's approval. You can understand and imagine that when you are about to quit, you are clearly arranged to be signed and released by the leaders.

  • Command mode

The command mode is one of the behavior modes. The command object can be passed to the caller in the form of a constructor in a data-driven manner. The caller then provides the corresponding implementation to provide operation methods for command execution. You may feel that this part is a little bit rich, you can understand the code implementation, and then become proficient through practical operations.

  • Iterator mode

The advantage of the iterator mode is that it allows us to traverse different data structure elements in the same way. These data structures include: arrays, linked lists, trees, etc., and users do not need to care about each data structure when using traversal The traversal processing logic has changed from making the use uniform and easy to use.

  • Intermediary model

What the intermediary model needs to solve is the repeated calls between complex function applications, and a layer of intermediary packaging services is added in the middle to provide simple, universal, and easy-to-expandable service capabilities.

  • Memo mode

The memorandum mode is a design mode that can be restored or rolled back, configuration, version, and repentance are the core functions, and this design mode is a behavior mode. In terms of function realization, the memo operation class is added based on not destroying the original object, and the behavior of the original object is recorded to realize the memo mode.

  • Observer mode

To put it simply, the observer mode means that when a behavior occurs, it transmits information to another user to receive and perform corresponding processing.
There is no direct coupling relationship between the two . For example: Sniper, Li Yunlong.

  • State mode

The status mode describes multiple status changes under one behavior. For example, on our most common website page, the content displayed when you log in and not log in is slightly different (you cannot display personal information without logging in). This kind of login and non-login is that we change the whole behavior by changing the state.

  • Strategy mode

Strategy mode is a behavioral mode, and it is also a weapon to replace a large number of ifelse. What it can help you solve is the scenario, which is generally a scenario with alternative behavioral logic algorithms of the same kind. For example: different types of transaction methods (credit card, Alipay, WeChat), unique ID strategy (UUID, DB self-increment, DB+Redis, snowflake algorithm, Leaf) algorithm, etc., can all use strategy mode for behavioral packaging for external use .

  • Template mode

The core design idea of ​​the template pattern is to define the execution order of abstract methods in abstract classes, and set the abstract methods to be implemented by only subclasses, but not to design independent access methods. Simply put, it means to arrange you clearly.

  • Visitor mode

The core issue to be solved by visitors is to add variable business access logic under a stable data structure, such as user information and employee information. In order to enhance scalability, a design pattern that decouples the two parts of the business.

[Java design pattern practice guide] Based on the practical development of real scenes on the Internet, the learning of each module will involve 1-3 corresponding case projects, which are mentioned in each module of the document. You can refer to the control during the process.

After learning, summarize the knowledge points, and think about how to use such design patterns in your own business scenarios. Code friends who feel helpful, please don't be stingy with your likes. Your support is my motivation.

Quick start channel: ( click here ) to download! Full of sincerity! ! !

Selected questions for Java interviews, architecture and actual documentation portal: https://docs.qq.com/doc/DRW1nUkdhZG5zeGVi

Guess you like

Origin blog.csdn.net/weixin_45784983/article/details/108171568