One-sentence Design Patterns - Quickly Master the Core Ideas of 23 Design Patterns (Part 1)

This article is not suitable for deep learning of design patterns, because there is no code, no pattern extension.

The correct way to use it: 1. Beginners leave a rough impression before going into depth. 2. A quick review for those who have systematically learned design patterns.

Simple Factory Pattern

Core idea: separate the creation of classes from the use of objects.

Specific implementation process: define a factory that returns instances of different classes according to the parameters (usually strings). Because we want the objects produced by the factory to be handled in the same way, product classes usually implement the same interface.

Factory Method Pattern

Core idea: On the basis of a simple factory, different classes are created by instantiating different factories.

Specific implementation process: On the basis of a simple factory, abstract the factory, and delay the creation of the class to the subclass of the factory. The advantage of this is that when you need to add a new product class, you only need to add a "concrete factory" instead of modifying the specific implementation of the factory class like a simple factory. Comply with the "open-closed principle".

Abstract Factory Pattern

Core idea: Based on the factory method, let a factory produce a class of objects.

Specific implementation process: On the basis of the factory method, the method of producing different products is realized in a specific factory. It can achieve the purpose of facilitating the production of a "product cluster".

builder mode

Core idea: define a construction process, and place the specific construction in the subclass, so that different instances can be constructed using the same construction process.

prototype mode

Core idea: Use the clone method of the Object class to quickly copy objects.

singleton pattern

Core idea: Let a class itself hold the member variables of its instance, declared as staticsum private, to achieve the purpose of only one instance in the system.

 

Specific implementation process: In order to ensure that there is only one instance in the whole system, so users cannot create it themselves, so this class needs a privateconstructor. Also need a getInstance()method to get this unique instance, because it needs to be available everywhere in the system, so this method should be public. Because getInstance()the purpose of the call is to get the instance, it can only be called on the class, so it getInstance()should be static.

adapter mode

The core idea: to create an adapter class can have the behavior of the existing class, and the desired interface.

Specific implementation process: divided into two cases

  1. Class adapter: The adapter class implements the desired interface, inheriting the existing class has acquired his behavior (methods).
  2. Object Adapter: The adapter class implements the desired interface and holds an instance of the existing class through which methods are invoked. The purpose is the same.

bridge mode

Core idea: use combination or aggregation to deal with the connection between abstraction and implementation, so that the specific implementation changes in different dimensions

Combination mode

Core idea: Let concrete classes and containers of these classes implement the same interface, and users use recursive calls when calling container classes, and there is no need to distinguish the difference between the two.

Appearance Mode

Core idea: To implement specific operations in the appearance class, the user only needs to deal with the appearance class and shield the method invocation details.

flyweight pattern

Core idea: Build an object pool to reuse objects that need to appear multiple times in the system.

decorative pattern

The core idea: , the decorator and the decorated object have the same interface, the decorator obtains the decorated object when it is used, and adds new operations to it.

Specific implementation process: Whether it is a specific component or a decorated component, it is hoped that it can be used in the same way, so the same interface is implemented. The decorated class needs to hold an instance of a specific component in order to call its methods, so an aggregate association is required.

proxy mode

Core idea: The proxy class holds an instance of the concrete implementation class, and converts the operation on the proxy class into a method call on the instance.

 

 

 

(Original address: https://www.jianshu.com/p/dd2bbbbcddab. Respect originality, thank the author!)

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520534&siteId=291194637