“This is the 27th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”

1. What design patterns do you know

There are generally believed to be 23 design patterns in Java. We don’t need all of them, but there are a few common design patterns that we should learn. All design patterns are listed below. The design patterns that need to be mastered are listed separately, as many as possible.

Generally speaking, design patterns can be divided into three categories:

There are five types of creation patterns: factory method pattern, Abstract factory pattern, singleton pattern, Builder pattern, and prototype pattern.

Structural mode, a total of seven: adapter mode, decorator mode, agent mode, appearance mode, bridge mode, composite mode, share mode.

There are eleven behavioral modes: strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, command mode, memo mode, state mode, visitor mode, intermediary mode, interpreter mode.

2. Singleton design pattern

One of the best understood design patterns, divided into slacker and hungry

The hungry type:

Public static Singleton instance = newSingleton(); Private Singleton(){} public static Singleton getInstance(){return instance; }}Copy the code

LanHanShi:

Public class Singleton{private static volatile Singleton = null; Private Singleton(){} public static Singleton getInstance(){if(Singleton == null){ synchronized(Singleton.class){ id(singleton == null){ singleton = new Singleton(); } } } return singleton; }}Copy the code

3. Factory design mode

Factory pattern is divided into factory method pattern and abstract factory pattern.

Factory method pattern

There are three factory method patterns:

The normal factory pattern is to create a factory class that creates instances of classes that implement the same interface.

The multiple factory method pattern is an improvement over the normal factory method pattern, where an object cannot be created correctly if the string passed is wrong, whereas the multiple factory method pattern provides multiple factory methods to create objects separately.

Static factory method mode, set the above multiple factory method mode methods to static, do not need to create instances, just call.

4. Builder mode

The factory class pattern provides a pattern to create a single class, while the builder pattern is to centralize the management of various products to create composite objects. The so-called composite objects mean that a class has different attributes. In fact, the builder pattern is the combination of the abstract factory pattern and the final Test.

5. Adapter design pattern

The adapter pattern translates the interface of a class into another interface representation that the client expects, in order to eliminate class compatibility problems caused by interface mismatches. There are three main categories: the class adapter pattern, the object adapter pattern, and the interface adapter pattern.

6. Decoration mode

As the name implies, decoration mode is to add some new functionality to an object, and it is dynamic. It requires that the decoration object and the decorated object implement the same interface, and the decorated object holds an instance of the decorated object.

7. Strategic mode

The policy pattern defines a series of algorithms and encapsulates each algorithm so that they can be replaced without affecting the customers using the algorithm. You need to design an interface that provides a unified approach for a series of implementation classes, multiple implementation classes that implement the interface, and an abstract class (optional, a helper class) that provides helper functions. The decision of the strategy mode is up to the user. The system itself provides the realization of different algorithms, adding or deleting algorithms, and encapsulating various algorithms. Therefore, the strategy mode is often used in algorithmic decision system, and external users only need to decide which algorithm to use.

8. Observer mode

The observer model is easy to understand. It is similar to email subscriptions and RSS subscriptions. When we browse some blogs or wikis, we will often see an RSS icon, which means that when you subscribe to the article, you will be notified of any subsequent updates. In a nutshell: when an object changes, all other objects that depend on it are notified, and with the change! Objects have a one-to-many relationship.