Definition: provides an interface for creating a set of related or interdependent objects without specifying their specific classes.
Type: Create class Mode
Class Diagram:
The difference between abstract Factory mode and factory method mode
Abstract Factory mode is an upgraded version of the factory method pattern that is used to create a set of related or interdependent objects. The difference between him and the factory method pattern is that the factory method model is for a product hierarchy, while the abstract factory model is for multiple product hierarchy structures. in programming, typically a product structure is represented as an interface or abstract class, that is, all products provided by the factory method pattern are derived from the same interface or abstract class, and the products provided by the abstract factory schema are derived from different interfaces or abstract classes.
In the abstract factory model, there is a product family concept: the so-called product family, refers to the different products in the hierarchical structure of the functions associated with the family of products. the Abstract Factory model provides a series of products that form a product family, while the factory approach provides a range of products called a hierarchical structure. We still use the example of producing cars to illustrate the difference between them.
In the above class diagram, the hatchback and sedan are called two different hierarchical structures , while 2.0-displacement vehicles and 2.4-displacement vehicles are called two different product families. A little more specific, 2.0-Displacement hatchback and 2.4-displacement hatchback belong to the same grade structure, 2.0-displacement sedan and 2.4-displacement sedan belong to another grade structure, while the 2.0-displacement hatchback and 2.0-displacement sedan belong to the same product family, 2.4-displacement hatchback and 2.4-displacement sedan belong to another product family.
Understand the concept of hierarchical structure and product family, understand the difference between the factory method pattern and the abstract factory model, if the factory products all belong to the same hierarchical structure, it belongs to the factory method mode; If the factory product comes from multiple hierarchies, it is an abstract factory pattern. in this case, If a factory model offers a 2.0-displacement hatchback and a 2.4-displacement hatchback, then he belongs to the factory method model; If a factory model is to offer 2.4-displacement hatchback and 2.4-displacement sedan two products, then this factory model is abstract Factory mode, because he provides products are divided into two different hierarchical structure. Of course, if a factory provides all four models of products, because the product is divided into two hierarchical structure, he certainly belongs to the abstract factory model.
Abstract Factory mode Code
Interface IProduct1 { public void Show (); } |
Interface IProduct2 { public void Show (); } |
class Product1 implements IProduct1 { public void Show () { SYSTEM.OUT.PRINTLN ("This is a Type 1 product"); } |
Class Product2 implements IProduct2 { public void Show () { System.out.println ("This is a Type 2 product"); } } |
Interface Ifactory { Public IProduct1 CreateProduct1 (); Public IProduct2 createProduct2 (); } |
class Factory implements ifactory{ Public IProduct1 CreateProduct1 () { return new Product1 (); } public IProduct2 CreateProduct2 () { return new Product2 (); } } |
public class Client { public static void Main (string[] args) { Ifactory factory = new Factory (); Factory.createproduct1 (). Show (); Factory.createproduct2 (). Show (); } } |
Advantages of the abstract factory model
Abstract Factory mode In addition to the advantages of factory method mode, the main advantage is that the product family can be constrained within the class . The so-called product family, generally more or less there is a certain association, the abstract factory model can be within the class to define and describe the relationship of the product family, rather than introducing a new class to manage.
Disadvantages of the abstract factory model
Product family expansion will be a very laborious thing, if the product family need to add a new product family, then almost all of the factory classes need to be modified. Therefore, when using the abstract factory model, it is very important to classify the product hierarchy.
Applicable scenarios
Abstract Factory mode can be used when objects that need to be created are a series of interrelated or interdependent product families. To be more clear, it is an inheritance system, if there are multiple hierarchical structures (that is, there are multiple abstract classes), and there are certain associations or constraints between the implementation classes in each hierarchy, you can use the abstract factory pattern. If there is no association or constraint between the implementation classes in each hierarchy, it is more appropriate to create the product using multiple independent factories.
Summarize
Whether it's a simple factory model, a factory method model, or an abstract factory model, they all belong to the factory model and are very similar in form and feature, and their ultimate goal is to understand the decoupling. In use, we don't have to care whether this pattern is a factory method or an abstract factory pattern, because the evolution between them is often elusive. Often you will find that the factory method is clearly used, when the new requirements come, slightly modified, adding a new method, because the products in the class constitute a different hierarchical structure of the product family, it becomes an abstract Factory mode, and for the abstract factory model, when the reduction of a method of the provided products no longer constitute the product family, It evolved into a factory method model.
Therefore, when using the factory model, it is only necessary to care whether the purpose of reducing coupling is achieved.
(3) Abstract Factory mode