I haven't written a blog for a long time, and I have always felt that I have been tapping into the design pattern.CodeSuddenly, I feel a little less summarized! Now the code is basically finished, and now the module is simply summarized:
First, let's take a look at the Creation Mode in the design mode: it is used to process object creation and try to create an object in the appropriate way according to the actual situation. It is actually creating a new class or object.
1. factorymethod)
1. Definition: defines an interface for creating objects, so that the subclass decides which class to instantiate. The factory method delays the instantiation of a class to its subclass.
2, corresponding to its structure:
3. Compare with a simple factory: when the factory method mode is implemented, the client needs to decide to instantiate the factory to implement the computing class and select whether the problem still exists. That is to say, the factory method moves the internal logic judgment of the simple factory to the client code. You want to add a function, which is originally of the factory class, but now you want to modify the client.
4. Application Example: Learning from Lei Feng to help others
·
2. abstractfactory)
1. Definition: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
2. Basic Structure:
3. Advantages and Disadvantages:
Advantages: A, easy to switch product series. Because of the specific factory class, it only needs to appear once during initialization in an application, which makes it very easy to change the specific factory of an application, you only need to change the specific factory to use different product configurations. b. It separates the instance creation process from the client. The client uses their abstract interfaces to manipulate the instance, and the product classes are also separated by the specific factory implementation, does not appear in customer code.
Disadvantage: too many classes need to be modified to add functions inside!
4. Application Example: switch between different databases.
Iii. Prototype)
1. Definition: Use a prototype instance to specify the type of the object to be created and copy the prototype to create a new object.
2. Basic Structure:
3. Note: The prototype mode is to create another custom object from an object without any details. On the. NET platform, the icloneable interface is provided to directly implement the prototype. The copied value type is not cloned for the reference type. It needs to be implemented through deep replication (pointing the variables of the referenced object to the copied new object.
4. Application Example: print multiple resumes.
4. Builder mode ):
1. Definition: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
2. Basic Structure:
3. Description: it is mainly used to create complex objects. The building sequence between these objects is usually stable, but the building inside the object is usually subject to complex changes. When a complex object is createdAlgorithmIt should be independent of the organizational part of the object and a mode used in their assembly mode.
4. Application Example: draw complicated processes for people and cooking.
5. Singleton)
1. Definition: ensure that a class has only one instance, and provide a global access point to access it.
2. Basic Structure:
3Note: The class itself is responsible for saving its unique instance. This class can ensure that no other instance can be created, and it can provide a method to access the instance and allow controlled access.
4For example, only one toolbox can be displayed.
In summary, the five design patterns in the creation model are simple definitions and class diagrams. The real understanding still requires constant practice.