Java Development-Abstract Factory and abstractfactory
As the twin brother of the factory method model, I believe you are confused about the factory method model and the abstract factory model.
Let me save you!
Abstract Factory Model
Definition:The abstract factory mode provides an interface for creating a group of related or mutually dependent objects without specifying their specific classes.
Type:Create Class Mode
Class diagram:
Differences between Abstract Factory mode and factory method mode
Abstract Factory mode is an upgraded version of the factory method mode. It creates a group of related or mutually dependent objects. The difference between the factory method model and the factory method model is that the factory method model is for a product level structure, while the abstract factory model is for multiple product level structures. In programming, a product structure is usually represented as an interface or abstract class. That is to say, all products provided by the factory method mode are derived from the same interface or abstract class, the abstract factory model provides products derived from different interfaces or abstract classes.
In the abstract factory model, there isProduct FamilyConcept: the so-called product family refersIt is located in the family of products associated with functions in different product level structures.Abstract A series of products provided by the factory model constitute a product family, and a series of products provided by the factory method are called a hierarchical structure. Let's take the example of producing cars to illustrate the differences between them.
In the above class diagram, the hatchback and the sedan are called two different hierarchical structures, while the 2.0 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 and 2.4-displacement hatchback belong to another level structure; while 2.0-displacement hatchways and 2.0-displacement sedan cars belong to the same product family, 2.4-displacement hatchways and 2.4-displacement sedan cars belong to another product family.
After understanding the hierarchical structure and product family concepts, we can understand the differences between the factory method model and the abstract factory model. If all the factory products belong to the same hierarchical structure, they belong to the factory method model; if the factory product comes from multiple hierarchical structures, it belongs to the abstract factory model. In this example, if a factory model provides a 2.0-displacement hatchback and a 2.4-displacement hatchback, it belongs to the factory model; if a factory model provides two products: A 2.4-displacement hatchback and a 2.4-displacement sedan car, the factory model is an abstract factory model because the products it provides belong to two different levels. Of course, if a factory provides products of all four models, it also belongs to the abstract factory model because the product belongs to two hierarchical structures.
To sum up, the difference between them is:
Factory method mode:
An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.
Abstract Factory mode:
Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class, that is, multiple products under a product line.
Differences:
Abstract factory pattern 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 type 2 Product") ;}} interface ifacloud {public IProduct1 createProduct1 (); public IProduct2 createProduct2 ();} class Factory implements ifacloud {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 abstract factory Model
Abstract Factory mode not only has the advantages of factory method mode, but also has the primary advantage of being able to restrict the product family within the class. The so-called product family generally has a certain degree of Association. The abstract factory model can define and describe the association between product families within the class, instead of introducing a new class for management.
Disadvantages of abstract factory Model
Product Family expansion is 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, the classification of product hierarchy is very important when abstract factory models are used.
Applicable scenarios
Abstract Factory mode can be used when the object to be created is a series of interrelated or interdependent product families. A more clear point is that in an inheritance system, if there are multiple levels (that is, there are multiple abstract classes ), the abstract factory mode can be used if there are certain associations or constraints between implementation classes in each hierarchy. If there is no association or constraint between implementation classes in each hierarchy, it is more appropriate to create products using multiple independent factories.
Summary
Both the simple factory model, the factory method model, and the abstract factory model belong to the factory model. The form and characteristics are extremely similar. Their ultimate goal is to decouple them. In use, we don't have to worry about whether this mode is factory method mode or abstract factory mode, because the evolution between them is often unpredictable. You will often find that the factory method mode is clearly used. When a new requirement comes and a slight modification is made, after a new method is added, because the products in the class constitute the product family with different levels of structure, it becomes the abstract factory model. For the abstract factory model, when a method is reduced to make the provided product no longer constitute a product family, it becomes the factory method model.
Therefore, when using the factory model, you only need to care about whether the goal of reducing the coupling degree has been achieved.