1. Simple Factory mode
How to understand simple factory, factory method, abstract factory three design patterns?
Simple factory life scene, sell breakfast small vendors, he gives you buns, steamed bread, gutter oil pancake, etc., Hawker is a factory, it produces steamed buns, steamed bread, cooking oil pancake. The UML diagram corresponding to this scenario is as follows:
Figure 1: Simple Factory mode UML diagram
Participants in the Simple factory model:
Factory ( Factory role: Accepts the client's request and is responsible for creating the corresponding Product object through the request.
Abstract Products ( Abstract Product ) Role : is the parent or co-owned interface of the object created by the Factory mode. But abstract classes or interfaces.
Specific Products ( concreteproduct object: The object created by Factory mode is an instance of this role.
The evolution of the Simple factory model:
1.) when there is only one product in the system, you can omit the abstract product,as shown in1 . In this way, factory roles and specific products can be combined.
Advantages and disadvantages of the simple factory model:
1.) The factory class contains the necessary logic to create the product, so that the client only needs to request the product, without regard to the implementation details of the product.
2.) The factory class has only one, it concentrates all the product creation logic, it will be the entire system bottleneck, simultaneously causes the system to be difficult to expand.
3.) Simple Factory mode typically uses a static factory approach, which makes it impossible for a factory class to inherit from subclasses, which makes it impossible for a factory role to form an inheritance-based hierarchy.
2. Factory Method Mode
Factory Method Use OOP Polymorphism , the factory and the product are abstracted out of a base class, a unified interface is defined in the base class, and then a specific product is created in the specific factory. Factory method of life scene, Unilever to produce "Hazeline" and "Qingyang " two shampoo, it will build a production "Hazeline " of the factory and a production of "Qingyang " 's factory.
Figure 2: UML Diagram of the factory method
Participants in the factory method pattern:
Abstract Factory Role: regardless of the application, any factory that creates objects in the schema must implement this interface.
Specific factory roles: a concrete class that implements an abstract factory interface, contains logic that is closely related to the reference, and is called by the application to create the Product object.
Abstract Product Roles: The super type of the product object created by the factory method, which is the common parent of the product object or the co-owned interface.
Specific product roles: This role implements an interface that is known to abstract product roles. Each specific product object created by the factory method is an instance of a specific product role.
Advantages and disadvantages of the factory approach:
1.)It reduces the cohesion of the factory class, satisfies the hierarchical relationship between classes, and conforms to the single responsibility principle in object-oriented design, which is advantageous to the extension of the program, and three shows:
Figure 3: Extension of the factory method UML diagram
Summary: put " Commonality " extracted, according to the respective " Personality " The realization of establishing their respective inheritance commonalities
3. Abstract Factory design pattern
The so-called abstract factory refers to a factory hierarchy that can create all objects in a product family that belong to a different product hierarchy to create Unix controls, and Windows control As an example, we need an abstract factory below there are two sub-factories, a call Unixfactory, for production Unix family control, which is called a winfactory , for production Win family control. The difference between an abstract factory and a factory method is that a specific factory in a factory method typically produces only one or several control objects, whereas a specific factory in the abstract factory produces a family of control objects. As shown in 4.
Figure 4: Abstract Factory design Pattern UML diagram
Participants in the abstract factory:
Abstract Factory ( Abstract Factory ) Role: This role is at the heart of the factory approach model, which is independent of the business logic of the application system.
Specific Factory ( Concrete Factory ) Role: This role creates an instance of the product directly under the client's call. This role contains the logic to choose the right product object, which is closely related to the business logic of the application system.
Abstract Products ( Abstract Product ) Role: The classes that assume this role are the parent classes of objects created by the factory method pattern, or the interfaces that they collectively own.
Specific Products ( Concrete Product ) Role: any Product object created by the abstract factory pattern is an instance of a specific product class. This is what the client ultimately needs, and its internals must be filled with the business logic of the application system.
Usage scenarios for abstract factories:
- A system should not rely on the details of how a product class instance is created, composed, and expressed, which is important for all forms of Factory mode.
- This system has more than one product family, and the system only consumes one of the product families.
- Products belonging to the same product family are used together, and this constraint must be reflected in the design of the system.
- The system provides a library of product classes, with all products appearing on the same interface, so that clients do not rely on implementations.
The difference between abstract Factory mode and factory method mode
Factory method Mode: Each abstract product derives multiple specific product classes, each abstract factory class derives from multiple specific factory classes, and each specific factory class is responsible for the creation of an instance of a specific product;
Abstract Factory mode: Each abstract product derives from multiple specific product classes, each of which derives from multiple specific factory classes, each of which is responsible for multiple ( A range ) instance creation of the specific product.
In the Uikit framework, we use the factory method and the abstract factory two design models to achieve two demo, do not understand the two design patterns how to achieve the friend can download here: Brush not out, later to fill!
Reference article:
Simple factory: http://blog.csdn.net/z251257144/article/details/7476849
Factory Method: http://blog.csdn.net/z251257144/article/details/7479183
Abstract Factory: http://blog.csdn.net/z251257144/article/details/7482810
iOS design mode-factory method (Simple Factory mode, factory method mode, abstract Factory mode)