2. product level structure and product family
In the factory method mode, a specific factory is responsible for producing specific products. Each factory corresponds to a specific product. The factory method is unique. Generally, A specific factory has only one or more heavy-duty factory methods. But sometimes we want a factory to provide multiple product objects, rather than a single product object. For example, an electrical appliance factory can produce televisions, refrigerators, air conditioners, and other electrical appliances, instead of producing only one electrical appliance. To better understand the abstract factory model, we first introduce two concepts:
(1)Product Level Structure:The product level structure is the product inheritance structure.For example, if an abstract class is a TV set and its sub-classes include Haier TV, Hisense TV, and Tcl TV set, a product grade structure is formed between the abstract TV set and the TV set of a specific brand, abstract TV sets are the parent class, while TV sets of specific brands are sub-classes.
(2) Product Family: In the abstract factory model,A product family refers to a group of products in different product levels produced by the same factory.Such as Haier TV set and Haier refrigerator produced by Haier Electric Appliance Factory. Haier TV set is located in the TV product level structure, and Haier refrigerator is located in the refrigerator product level structure, haier TV sets and Haier refrigerators constitute a product family.
The product level structure and product family 3 are as follows:
Figure 3 product family and product level structure
In Figure 3, multiple squares, circles, and rectangles of different colors constitute three different product grade structures, respectively, square, circle, and elliptical shapes in the same color constitute a product family. Each shape object is located in a product family and belongs to a product grade structure. Figure 3 contains five product families, which are divided into three different product levels. You only need to specify the product family of a product and its hierarchical structure to uniquely identify the product.
The abstract factory model can be used when the factory-produced products provided by the system are not a simple object, but multiple specific products that are located in different product levels and belong to different types.Abstract Factory mode is the most abstract and general form in all forms of factory mode.The main difference between the abstract factory mode and the factory method mode is that the factory method mode is aimed at a product level structure, while the abstract factory mode needs to face multiple product level structures, A factory level structure can be used to create multiple product objects in different product levels.When a factory level structure can create all objects in a product family with different product levels, the abstract factory mode is simpler and more efficient than the factory method mode.The abstract factory mode 4 is shown below:
Figure 4 Abstract Factory Mode
In Figure 4, each specific factory can produce all products belonging to a product family, such as square, circle, and elliptical shapes with the same color, the products produced are in different product level structures. If the factory method mode is used, the structure shown in Figure 4 requires 15 specific factories, while the abstract factory mode requires only five specific factories, greatly reducing the number of classes in the system.
【Author】 Liu WeiHttp://blog.csdn.net/lovelion]