Design patterns are divided into three main types: creation, structure and behavior. This article describes the creation pattern.
The creation pattern includes abstract Factory mode, builder mode, factory method mode, prototype mode, and Singleton mode.
Abstract Factory Mode: provides an interface to create a series of related or related dependent objects without specifying their specific classes.
Benefits: (1) Easy-to-exchange product line, due to the specific factory class, in one application only need to appear at the time of initialization, which makes it very easy to change the specific factory of an application, it only needs to change the specific factory can use different product configurations. (2) It lets the concrete creation instance process and the client detach, the client is manipulates the instance through their abstract interface, the product specific class name also is separated by the concrete factory implementation, does not appear in the customer code.
Builder Mode: separating the construction of a complex object from its representation allows the same build process to create different representations.
Features: Avoid omissions and defects, ensure the integrity and standardization of specific structures.
Use: Defines an abstract class that writes the required structure as an abstract method in an abstract class. Abstract methods using abstract classes use the premise that all abstract methods in the abstract class must be overridden without a single omission.
Factory Method Mode: defines an interface for creating an object, letting subclasses decide which class to instantiate. The factory pattern defers the instantiation of a class to its subclasses.
Each model is a solution to a problem, and the factory approach model is for a product hierarchy, while the abstract factory model is for multiple product hierarchy structures.
prototype mode: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.
Features: Copy the template to use, for the personalized content, just set at the property.
Use: Create an entire template with properties, and you can set the initial value of the field so that when set, the empty content will be filled with an initial value.
Singleton mode: guarantees that a class has only one instance and provides a global access point to access it.
Features: Display construction method, construction will not get out of control, everything in control.
Benefit: The singleton mode, in addition to guaranteeing a unique instance, strictly controls how the customer accesses it and when to access it. In short, controlled access to the only instance.
Creation mode of design pattern