First, Abstract Factory Factory ) mode
Abstract Factory mode is the most abstract and general form of the factory pattern in all forms.
In order to facilitate the introduction of the abstract factory model, a new concept is introduced: product familyFamily. The so-called product family, is located in the different product hierarchy structure, the function is related to the product composition family.
A total of four product families in the figure, distributed in three different product hierarchy. This product can be determined only by specifying the product family in which a product is located and the hierarchy to which it belongs.
Introduction of abstract Factory mode
The so-called abstract factory refers to a factory hierarchy that can create all the objects in a product family that belong to different product hierarchy structures. If used to describe, such as:
Second, Abstract Factory pattern roles and structures
The products described in the figure are described in the product family as follows:
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.
Third, Under what circumstances is abstract Factory mode used
The abstract factory pattern should be used in the following cases:
- 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.
Four, The origin of the abstract factory
It is said that the earliest application was to create a system that could run in a Windows environment of different operating systems. For example, in Windows and UNIX systems have Windows environment components, in each operating system, there is a component family of Windows components. We can give the function description through an abstract role, and the specific subclass gives the specific implementation under different operating systems,
It can be found that the above product class diagram has two product hierarchy structures, button and text respectively, and two product families: the UNIX product family and the Windows product family.
The system's creation requirements for product objects are met by the hierarchy of a factory. There are two specific plant roles, namely Unixfactory and Winfactory. The Unixfactory object is responsible for creating products in the Unix product family, and Winfactory is responsible for creating products in the Windows product family.
It is obvious that a system can only run in a Windows environment of one operating system and not on different operating systems at the same time. Therefore, the system can actually only consume products belonging to the same product family.
In modern applications, the use of abstract Factory mode has been greatly expanded, no longer require the system to consume only one product family.
Five, Another example of an abstract factory (how to design an abstract factory to stay for thought)
Six, "open-closed" principle
The "open-closed" principle requires that the system be open to expansion and closed to modifications. The extension achieves the purpose of enhancing its functionality. For systems that involve multiple product families and multiple product hierarchies, the enhancements include two aspects:
- Add Product family: Abstract Factory a good support for the "open-closed" principle.
- Increase the hierarchy of new products: all plant roles need to be modified, and the "open-closed" principle is not well supported
In combination, the abstract factory model supports the addition of new products in a slanted manner, which facilitates the addition of new product families, and does not provide such convenience for the addition of additional grades.
Source code:http://pan.baidu.com/s/1gdsDaAf
Abstract Factory (Factory) mode