Design Patterns study notes (b) - creator of the model (Creational Pattern)
Others
2019-08-30 07:35:11
views: null
Creator mode (Creational Pattern)
- Simple factory pattern (Simple Factory)
- Also known as static factory method pattern, it does not belong to one of 23 kinds of GOF design patterns
Factory.creteObject(objectName);
- Factory method model (plant model) (Factory Method)
- A class does not know it needs an object of class: the client does not need to know the class name specific product category, you only need to know the corresponding plant
- A class to specify which objects created by its subclasses
- The delegate tasks to create objects of multiple plants in one sub-category, sub-category and then dynamically assigned as needed
FactoryA.creteObject();
FactoryB.creteObject();
- Abstract factory mode (Mode Kit) (Abstract Factory)
- Creating a series of related or dependent object's interface, without specifying their concrete classes
(factory pattern generating only one object, abstract factory model generates a set of related objects)
- When each of the abstract factory model class concrete factory creates only a target product, i.e. there is only one knot grade product, abstract factory model degenerates into the factory method pattern;
- When the factory method abstract factory pattern combined with the plant-specific, providing a unified product factory to create objects and create objects as static factory method design methods, the factory method pattern degenerate into a simple factory pattern
interface PcFactory{
Mouse createMouse();
Keyboard createKeyboard();
}
class HpFactory implements PcFactory{
Mouse createMouse() return HpMouse();
Keyboard createKeyboard() return HpKeyboard();
}
class DellFactory implements PcFactory{
Mouse createMouse() return DellMouse();
Keyboard createKeyboard() return DellKeyboard();
}
- Builder mode (generating mode) (Builder)
- It represents the construct and its separation of a complex object, such that the same build process can create different representations
- The construction and performance of complex objects separated from the object, so that the same construction process can create different representations
//包含四个角色:
//Builder:抽象建造者;ConcreteBuilder:具体建造者;
//Director:指挥者;Product:产品角色
ConcreteBuilder builder = new ConcreteBuilder();
Director director = new Director();
director.setBuilder(builder);
Product pd = director.constuct();
pd.show();
- Prototype model (Prototype)
- Definition: Use prototype instance of the specified object type to create, create new objects by copying this example
- Application: Java using serialization and deserialization stream to achieve a deep clone
- Singleton pattern (the Singleton)
- Responsibilities singleton class too heavy, contrary to the "principle of the single function" to some extent.
Because both as a singleton class factory role, provides factory methods, but also serves as a product role, contains a number of business methods, the product features and product creation itself fused together
Origin www.cnblogs.com/kintanx/p/11432445.html