I. Problems solved by the creation mode and implementation methods
The main problem to be solved in the creation mode is the coupling between the class user and the class instance creation (new operation. That is to say, how to make the class user do not need to directly create an object in the code. In this way, when the class to be used needs to change, the user's Code does not need to change or requires only a small change. The changes of classes mentioned here are conditional. For example, from A to B, they must have A common abstraction. in code, they must have A common interface. In any field of problem, something is changing and something is stable. What we need to do is to separate the stable and the changing, so that the changing part will not lead to the changing part. We need to use interfaces to isolate the changes.
2. Explanations of various models
1. Single-piece Mode
Purpose: ensure that a class has only one instance and provide a way to access the instance. The following is a common implementation code:
1 Class DemoClass
2 {
3 private Object LockObj = new Object ();
4 private DemoClass instance;
5
6 private DemoClass ()
7 {}
8
9 public static DemoClass GetInstance ()
10 {
11 if (instance = null)
12 {
13 lock (LockObj)
14 {
15 if (instance = null)
16 {
17 instance = new DemoClass ();
18}
19}
20}
21
22 return instance;
23}
24}
The following explains why the 15th rows need to be null in a multi-threaded environment.
Assume that two threads A and B access this code, and the instance has not been created.
1. When instance is null at the beginning, assume that thread A and thread B are executed to row 3 at the same time.
2. At this time, thread A obtains the CPU time and starts to execute. Thread B waits here.
3. Thread A executes all the way down, creates an instance, and returns the result. Thread A ends, and the CPU time is handed over to thread B.
4. Because thread A ends, thread B gets the lock and does not have to wait any longer. Execute the 15th rows, because the instance has been created by thread A. If there is no null judgment, the instance will be created again.
2. Factory method mode
Intent: provide an interface for creating objects to delay class Instantiation to subclass.
Analysis: the focus is on the interface with the object to be created. It has only one Create method. The work of this method is very simple, that is, to Create an object, however, this job is done by the subclass of this interface. The Customer Code depends on this interface (or the Create method) and the IProduct interface of a specific product to work.
The structure is as follows:
3. creator Mode
Intention: separates the construction process of a complex object from its representation, so that different representations can be created for the same build process.
Analysis: The key here is the stability of the object building process. That is to say, the steps for creating an object are fixed, but the specific steps are different. The solution is to abstract the steps that need to be changed into an interface, and then build the process based on the instance of this interface without worrying about the specific implementation of these steps.
The structure is as follows:
In fact, the above ctor can be unnecessary. The building process of the object can also be placed in the Customer Code. This does not matter. Its existence will make the structure more loose.
4. Abstract Factory Model
Purpose: provide an interface for creating a series of related or mutually dependent objects without worrying about their specific implementations.
Analysis: the abstract factory mode is similar to the method used in the factory method mode. It provides an interface for creating an object, which delays the creation of a specific object to the subclass of this interface. The difference is that the factory method creates an object and a single object of a certain type. Abstract factories create a series of objects with certain associations or dependencies, there is a certain relationship between A1, B1, C1, D1 (or A2, B2, C2, D2). These objects are a group of related objects, they are not completely isolated.
The structure is as follows:
5. Prototype
Purpose: specify the type of the created object through the prototype instance, and then create a new instance by copying the prototype object.
Analysis: the implementation method and structure are relatively simple, that is, copying an object to create a new object.
The structure is as follows: