Factory Mode
Definition: Factory mode is the most commonly used instantiation object mode. It is a mode that uses factory methods instead of new operations.
Ordinary factory: factory is concrete, product is abstract. ★★★★★ ★★★★
Abstract factory: Factories are abstract and products are abstract. ★★★★★ ★★★★
Compare the factory pattern with the abstract factory pattern:
1. The factory model abstracts the Product and constructs the Product dynamically by passing ProductType.
【 more commonly used 】
2. Abstract Factory In the factory model, the factory is also abstract, abstract the behavior of the factory to provide products, so that different factories can be dynamically set up to produce a variety of different categories of products.
[Strip the coupling between factory and product for easy expansion]
The Singleton pattern
★★★★ ★★★★ ★★
Definition: The singleton ensures that there is only one instance of a class in the system. That is, a class has only one object instance.
The singleton pattern is probably the most common design pattern.
Main advantages:
1. Some classes are created frequently, which is a lot of overhead for large objects.
2, omit the new operator, reduce the frequency of system memory usage, reduce GC pressure.
3. Some types, such as printers, only need one or one printer globally.
Main disadvantages:
1. Because there is no abstraction layer in the simple interest mode and the coupling is too strong, it is very difficult to extend the singleton class.
2. The singleton class is too heavy, which violates the “single responsibility principle” to some extent.
3. Abuse of singletons will bring some negative problems. For example, in order to save resources, the database connection pool object is designed as a singleton class, which may lead to too many programs sharing the connection pool object and connection pool overflow. If an instantiated object is not used for a long time, the system considers it garbage and collects it, resulting in a loss of object state.
Therefore, think twice before using singletons.
Builder Mode
★★★★ ★★★★ ★★
Definition: To separate the construction of a complex object from its representation, so that the same construction process can create different representations.
The Builder pattern is similar to the factory pattern, but the difference is that the Builder pattern focuses on the assembly process of parts (the concrete creation is created inside the product), while the factory method pattern focuses on the creation process of parts.
Benefits of using the Builder pattern:
1. Using the Builder pattern can keep the client from having to know the details of the product’s internal composition (without requiring a lot of constructors).
2. The specific builder classes are independent of each other, which is very beneficial for system expansion.
3. Since the specific builder is independent, it is possible to gradually refine the construction process without any impact on other modules.
Where to use build mode:
1. When creating some complex objects, the building sequence among the internal components of these objects is stable, but the internal components of objects are facing complex changes.
2. The algorithm of the complex object to be created is independent of the components of the object and the assembly method of the components.
【Prototype】
★★★ ★★★ ★★★ ★★
Definition: The idea of this pattern is to take an object as a prototype, copy and clone it, and produce a new object similar to the original object.
Usage Scenarios:
(1) Some global configurations during network requests, such as: BaseUrl, Timeout, retryCount, cacheMode, Interceptor, etc. We don’t want to set these Settings every time we make a network request. Instead, we can store these Settings in a singleton. Then provide the method to set these properties dynamically in the network request, so that you can save a lot of setting operations, so as to achieve global configuration, dynamic change.
(2) In JS, object inheritance is implemented using the stereotype pattern (without extends), which was introduced after ES6.
Program source code
Github project source address :github.com/xuexiangjys… , remember to give a STAR if you feel helpful! ~ ~
Wechat official account
More information, welcome to wechat search public number: [My Android open source journey]