Reference URL: http://doc.okbase.net/luozhonglan/archive/103843.html
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 client requests and is responsible for creating the corresponding product objects through the request.
Abstract product Role: is the parent or co-owned interface of the object created by the factory pattern. But abstract classes or interfaces.
Specific product (concreteproduct) object: The object created by the 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 in 1. 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
The factory method uses the polymorphism of OOP to abstract the factory and the product into a base class, define a unified interface in the base class, and then create the specific product in the specific factory. Factory method Life scene, Unilever to produce "Hazeline" and "Qingyang" two shampoo, it will build a production "Hazeline" factory and a production "Qing" 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: concrete classes that implement an abstract factory interface, contain logic that is closely related to references, and are called by the application to create product objects.
Abstract Product Role: The Super-type of the product object created by the factory method, which is the common parent or co-owned interface of the product object.
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.) reduces the cohesion of the factory class, satisfies the hierarchical relationship between classes, and well conforms to the single principle of responsibility in object-oriented design, which is advantageous to the extension of the program, three shows:
Figure 3: Extension of the factory method UML diagram
Summary: The "generality" is extracted, according to their own "personality" to establish their respective inheritance of the realization of common
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 controls as an example, we need an abstract factory below there are two sub-factories, one called Unixfactory, For the production of Unix-family controls, one called Winfactory, for the production of win-family controls. 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 role: This role is at the heart of the factory approach pattern, which is independent of the application's business logic.
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 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 product (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 creating instances of multiple (series) specific products.
iOS Factory design mode