Definition: Provides an interface for creating a set of related or interdependent objects without specifying their concrete classes.
Type: Create class pattern class diagram:
The difference between abstract factory pattern and factory method pattern
Abstract factory pattern is an upgraded version of the factory method pattern, which is used to create a set of related or interdependent objects. The difference between it and the factory method model is that the factory method model targets a product hierarchy structure; while the abstract factory model targets multiple product hierarchy structures. In programming, a product structure usually manifests as an interface or abstract class. That is to say, all products provided by the factory method model are derived from the same interface or abstract class, while the products provided by the abstract factory model are derived from different interface or abstract class.
In the abstract factory model, there is a concept of product family: the so-called product family refers to a family of products composed of functionally related products in different product hierarchies. The series of products provided by the abstract factory model form a product family; while the series of products provided by the factory method is called a hierarchical structure. We still use the example of producing cars to illustrate the difference between them.
In the above class diagram, 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. To be more specific, the 2.0-displacement hatchback and the 2.4-displacement hatchback belong to the same level structure, the 2.0-displacement sedan and the 2.4-displacement sedan belong to another level structure; while the 2.0-displacement hatchback and the 2.0-displacement sedan belong to the same product family, and the 2.4-displacement hatchback and the 2.4-displacement sedan belong to another product family.
Once you understand the concepts of hierarchical structure and product family, you will understand the difference between the factory method model and the abstract factory model. If all the products of the factory belong to the same hierarchical structure, they belong to the factory method model; if the products of the factory come from multiple hierarchical structures, they belong to the abstract factory model. In this example, if a factory model provides a 2.0 displacement hatchback and a 2.4 displacement hatchback, then it belongs to the factory method model; if a factory model provides two products, 2.4 displacement hatchback and a 2.4 displacement sedan, then this factory model is an abstract factory model, because the products it provides belong to two different hierarchical structures. Of course, if a factory provides products of all four models, because the products belong to two hierarchical structures, it will certainly belong to the abstract factory model.
Example
Let's look at the abstract class example of factory class:
package AbstractFactory; public abstract class AbstractCreator { //Create A product method public abstract AbstractProductA createProductA(); //Create B product method public abstract AbstractProductB createProductB();}Abstract class of product A
package AbstractFactory; public abstract class AbstractProductA { //Method shared by Product A public void shareMethod(){ System.out.println("Business logic processing method shared by Product A..."); } //Method different sub-products of Product A public abstract void doSomething(); }Abstract Class of Product B
package AbstractFactory; public abstract class AbstractProductB { //Method shared by product B public void shareMethod(){ System.out.println("Business logic processing method shared by product B..."); } // Different sub-products of product B public abstract void doSomething(); } Factory implementation class package AbstractFactory; public class Creator1 extends AbstractCreator { @Override public AbstractProductA createProductA() { return new ProductA1(); } @Override public AbstractProductB createProductB() { return new ProductB1(); } } Factory implementation class package AbstractFactory; public class Creator2 extends AbstractCreator { @Override public AbstractProductA createProductA() { return new ProductA2(); } @Override public AbstractProductB createProductB() { return new ProductB2(); } }Product A1
package AbstractFactory; public class ProductA1 extends AbstractProductA { @Override public void doSomething() { System.out.println("Business logic processing method of product A1..."); } }Product A2
package AbstractFactory; public class ProductA2 extends AbstractProductA { @Override public void doSomething() { System.out.println("Business logic processing method of product A2..."); } }Product B1
package AbstractFactory; public class ProductB1 extends AbstractProductB{ @Override public void doSomething() { System.out.println("B1's business logic processing method..."); } }Product B2
package AbstractFactory; public class ProductB2 extends AbstractProductB{ @Override public void doSomething() { System.out.println("B2's business logic processing method..."); } } Advantages of abstract factory pattern
In addition to having the advantages of the factory method model, the most important advantage is that the product family can be constrained within the class. The so-called product family generally has a certain relationship to a greater or lesser extent. The abstract factory model can define and describe the relationship between product family within the class without having to introduce a new class specifically for management.
Disadvantages of abstract factory pattern
Expanding the product family will be a very laborious task. If a new product needs to be added to the product family, almost all factory classes need to be modified. Therefore, when using the abstract factory model, the division of product hierarchy structure is very important.
Applicable scenarios
Abstract factory patterns can be used when the objects to be created are a series of interrelated or interdependent product families. To put it more clearly, in an inheritance system, if there are multiple hierarchical structures (that is, multiple abstract classes exist), and there are certain correlations or constraints between implementation classes divided into each hierarchical structure, the abstract factory model can be used. It is more appropriate to use multiple independent factories to create products if there are no associations or constraints between implementation classes in each hierarchy.
Summarize
Whether it is the simple factory model, the factory method model, or the abstract factory model, they all belong to the factory model and are very similar in form and characteristics. Their ultimate goal is to decouple. When using it, we don’t have to care whether this pattern is factory method or abstract factory model, because the evolution between them is often difficult to understand. Often you will find that when a new requirement comes, a little modification is made and a new method is added, the products in the class form the product family in different hierarchical structures, it becomes the abstract factory model; and for the abstract factory model, when a method is reduced to make the products provided no longer form the product family, it evolves into the factory method model.
Therefore, when using the factory mode, you only need to care whether the purpose of reducing the coupling has been achieved.