Structural patterns:

Adapter mode:

It is often used to adapt a new interface to an old interface

In our business code, there is often a need for new and old interfaces, so we can adopt this mode.

Bridge mode:

Decoupling the abstraction from the concrete implementation of the abstraction allows the abstraction and the concrete implementation of the abstraction to change independently.

Fat toward small voice force force: this pattern, in fact, we use every day, but you may be unaware. Whenever you’re using interface oriented programming, you’re actually using bridge mode.

Portfolio model

To make it appear that the client handles both individual objects and combinations of objects equally; in other words, a method of a type also accepts its own type as an argument. (So in other words methods on a type of welfare the same type)

From the above sentence we can know that the combination mode is often used in the optimization of recursive operations, such as each company has a boss system, will have what menu function. For example, there are two menus under the first menu, and two menus have three menus. When deleting a primary menu, you need to constantly delete submenus, so you can try this design pattern. In short, you can try this design pattern with cascading operations.

Decorator pattern

Adding additional functionality to an object dynamically is an alternative to subclassing. This design pattern is widely used in the JDK, and the following are just a few examples

Fat toward whisper force: this pattern is used too widely, we commonly used AOP, both dynamic proxy, also has the flavor of decorator.

Facade pattern

Provides simplified interfaces for a set of components, interfaces, abstractions, or subsystems.

The SLFJ log we use every day is the facade log. For example, when we use Dubbo, the service we provide to the outside uses the facade mode as far as possible, and then the service calls various services to do aggregation.

The flyweight pattern

Use caching to reduce access time to small objects

Fat toward small voice force force: as long as used cache, basic are in the use of yuan model. Many students say that their project is too low, did not use any design pattern, this is not a joke, you use a map cache several objects, basically use the idea of sharing elements.

The proxy pattern

The proxy pattern is used to replace complex or time-consuming objects with simpler ones.

The proxy model is widely used, and almost every open source framework you know uses dynamic proxies.

Create a model

Abstract Factory pattern

The abstract factory pattern provides a protocol for generating a series of related or independent objects without specifying the type of a specific object. It enables the application to be decoupled from the concrete implementation of the framework in use. Ubiquitous in the JDK and many open source frameworks like Spring, they are easy to spot. Anything that creates objects but returns interfaces or abstract classes is the abstract factory pattern. (Any method that is used to create an object but still returns an interface or abstract class)

Fat toward the whisper force: from the English can be derived, this mode can be used in conjunction with the strategy mode.

Builder model

Used to simplify the creation of complex objects by defining a class whose purpose is to build an instance of another class. The Builder pattern also allows the Fluent interface to be implemented.

Fat mutters: This scenario is too widely used in our business code. For example, the order system has most projects, the order object is a complex object, we can use the builder mode to do.

The factory method

It’s just a method that returns the actual type.

Fat toward small voice force force: this belongs to everyone will design pattern, not much introduction.

The prototype pattern

Enables instances of classes to generate copies of themselves. If creating an instance of an object is too complex and time-consuming, you can use this pattern instead of creating a new instance, you can copy an object and modify it directly.

Fat toward small voice force force: this you think is the design pattern of cold door, actually wrong, this is big popular design pattern. For example, our business code often requires a variety of DTO, BO, DO, VO transformation, in fact, can refer to the idea of the prototype design pattern to DO.

The singleton pattern

Used to ensure that the class has only one instance. Joshua Bloch in Effetive Java suggests that another approach is to use enumerations.

In normal development, singletons are the ones we use the most, because Spring beans are singletons by default. Singletons are a design pattern that almost everyone knows.

Behavior patterns

Chain of responsibility

Decouple objects by passing requests from one object to the next in the chain until the request is processed. The objects in the chain are different implementations of the same interface or abstract class.

Fat toward small voice force force: all with Filter keyword, basically are using this design mode. There are so many scenarios in which business code is used that this design pattern is used almost everywhere interceptors are used.

Command mode

Wrap a command in an object so it can be stored, passed into a method, and returned just like any other object.

Fat toward small voice force: command mode use frequency is higher, and strategy mode is more like, the specific difference can be searched. If you have used the Activiti workflow engine, you can take a look at the source code, which uses command mode in many places.

Interpreter mode

This pattern is typically described as defining syntax for the language and using that syntax to interpret statements in that format. This pattern generally describes defining a grammar for that language and using that grammar to interpret statements in That format.)

Fat toward small voice force force: this compare cold door, fat toward didn’t how used, you used the words can leave a message to tell fat toward.

Iterator pattern

Provides a unified way to access objects in a collection.

This middleware and infrastructure group of students may use more, business code is not used, but the USE of JDK is very classic, can see.

The mediator pattern

Use an intermediate object for message distribution and to reduce direct dependencies between classes.

I don’t need to say anything more about this description. The business code uses too many scenarios. For example, if you use MQ, you are using the mediator pattern. Therefore, Fei Zhao repeatedly emphasizes that even if it is CRUD every day, paying attention to fei Zhao to study together can also give your CRUD project, plus beauty + filter (design mode) to enhance the effect.

Memo mode

Generates a snapshot of the object’s state so that the object can be restored to its original state without exposing its contents. For example, the Date object implements the memo pattern with an internal long value.

This is not used much in business, but as far as He knows, one of the scenarios is that you want to throw data to MQ, but MQ is temporarily unavailable, so you temporarily store the data to DB, and then poll to MQ. If you have a better scene, let Him know in the message.

Empty object mode

It allows you to abstract the handling of empty objects.

I don’t use much of this business code, but we do use some of these methods in the JDK.

Observer model

Used to provide a flexible way for components to broadcast messages to interested receivers.

Our business code is usually based on Zookeeper as an observer. Basically, ZK is used in observer mode, such as distributed locking, such as service discovery, etc.

The state pattern

Allows you to easily change the behavior of an object at run time based on its internal state.

Fat toward a small voice force force: this is too widely used in business code, I do not believe that your system has not “state”. For example, our common order status or various XX states can be used.

The strategy pattern

Use this pattern to encapsulate a set of algorithms into a series of objects. By calling these objects, you can change the function of the program flexibly.

This is too high frequency, often used to optimize a large number of if-else, if this design mode can not, out do not say that attention to the public number of fat!

Template method pattern

By allowing subclasses to override part of a method, rather than the whole thing, you can control which operations subclasses need to override.

Fat toward whisper force force: this mode is also very high frequency mode. Business code often has a lot of the same parts, so we can make an abstract class, subclass to differentiate, and if you don’t know that, do a quick search, again, very frequently.

Visitor pattern

Provides a convenient and maintainable way to manipulate a set of objects. It allows you to modify or extend the behavior of an object without changing the object on which you operate.

If you use it in the business code, please leave a message and tell it to me.

Write in the last