There are 23 gof design patterns, which can be divided into three categories: Creation type, structure type, and behavior type. This article mainly discusses creation type.
The design patterns of the creation model include: simple factory, factory method, abstract factory, Singleton, and builder) and prototype respectively.
First, let's look at the three design models of the factory series. They are mainly aimed at the "open-closed" principle in software design, that is, the program should be open to expansion and closed to modification. Especially when our programs use XML + reflection to create objects, the power of the factory mode is fully presented. In this case, we can maintain the configuration file, to control the logic of the program.
1)Simple FactoryWhen our program instantiates an object, if the input conditions are different and the generated objects are different, we can consider using a simple factory to uniformly encapsulate different instances. The UML structure is as follows:
Advantage: encapsulates the instantiation process of a specific object. The client end is decoupled from a specific object. At the same time, productmanager can be used as a static class or singleton object, then, you can use hashmap to cache a specific object (provided that the object has no time dependency) to reduce the number of object creation times.
Disadvantage: when adding a new type of object, you need to modify the productmanager code (if XML is not used)
2)Factory methodIt is a release version for a simple factory and adds an abstraction for productmanager. The UML structure is as follows:
Advantage: The structure is more flexible. For a certain type of object, there will be a specific object factory pointing to it, so when we need to add a new type of product, you only need to add two classes, one is the specific product class and the other is the factory class of the new product. This is more flexible.
Disadvantage: The structure becomes complex, and the client end needs to determine which factory to use (of course, this information can be saved in the context or configuration file ).
3)Abstract FactoryThis is the most complex factory model. It is used to generate all products on a product line. We assume that a product line contains multiple products, and the number of products on different products is the same, in this way, we need an abstraction for the product line, and it is clear that the online products of different products cannot be mixed together. The corresponding UML structure is as follows:
It indicates that an online product consists of iproduct1 and iproduct2. When the client obtains the product, the two products should return at the same time. Therefore, iproductmanager needs to generate both objects at the same time.
Advantage: the behavior of creating a product family is highly abstract, and the logic for adding a product line is clearer.
Disadvantage: When we add or delete products online, the corresponding operations are troublesome and all product factories need to be modified.
4)SingletonThis is a mode that is easy to understand. Literally, it means that during the running process of a program, only the unique instance of an object is reserved at any time. The corresponding UML structure is as follows:
The implementation of a singleton generally involves several steps: 1) Private pointing to its own fields; 2) private constructor; 3) Public instantiation of private fields. There are also several improvements for specific languages, such as adopting the double lock mechanism for multithreading, defining private fields using constants, and instantiating fields using embedded classes.
We can also make some proper extensions for the singleton. For example, we change the number of objects from one to N, which becomes the object pool.
The Singleton mode is usually used in the factory mode, especially for simple factories.
5)ConstructorFor some complex objects, it can be divided into multiple different parts. During instantiation, the order of Instantiation between different parts sometimes has strict restrictions, then we can use the constructor mode. The corresponding UML structure is as follows:
We define the ibuilder interface to instantiate different parts, and there is a method to return the instance of the object. The Construct Method of the constructor class calls the methods of instantiating some objects in sequence according to the business logic, that is, buildparta and buildpartb. The call sequence here is completely controlled by the business logic, finally, you can call the getproduct method to obtain the complete object instance.
We sometimes make changes, such as placing getproduct In the constructor or putting the Construct method into the getproduct (cancel the constructor. Even with these variants, the basic idea remains unchanged.
6)PrototypeWhen a new instance object is required during the program running, sometimes we do not want to create an object from scratch, the new instance is expected to be in the same status as an existing instance. This is where the prototype plays a role. The corresponding UML structure is as follows:
In. net, the iclonable interface has been defined to implement the prototype mode. Note that there is a difference between deep copy and shallow copy during implementation. Deep COPY Copies both stack and stack content, while shallow COPY Copies only stack content.