1. Abstract factory mode
- To solve the problem that the factory method pattern cannot create a group of related or interdependent objects, the abstract factory pattern is introduced
- Official definition of abstract factory pattern
It is a pattern structure that provides an interface for the access class to create a group of related or interdependent objects, and the access class can obtain different levels of products of the same family without specifying the specific class of the product.
Knock on the blackboard and draw the key points:
-
It provides an interface for creating a group of related or interdependent objects: compared with the factory method pattern, the abstract factory of the abstract factory pattern defines multiple interfaces for creating objects
-
Products of different grades:
- Different types of products are also called different grades of products.
- In the factory method mode, only the production of products of the same grade is considered, but there are many different models of products of the same grade
- Just like electronic factories only produce game consoles, there are many types of game consoles: game consoles with cards, game consoles with CDs, and portable game consoles (those playing Tetris) 😂)
-
Products of the same family
- In real life, electronic factories not only produce game consoles, but also produce televisions, speakers and other products.
- These different grades of products produced by a factory are called product families
- Therefore, different levels of products of the same family refer to that a factory can produce multiple types of products, echoing the previous interface to create a group of related or interdependent objects
-
Taking Haier and TCL as examples, the schematic diagram of product family and product grade is as follows
-
To sum up:
- In the abstract factory model, factories no longer produce only one kind of products, but can produce multiple kinds of products
- That is, factories can support the creation of multiple types of objects
- The caller can create multiple types of objects through the factory class without specifying the object type (specific class)
2. UML diagram
- The UML diagram of the abstract factory pattern is as follows:
- Abstract Factory: it defines multiple interfaces for creating products, and can create multiple products of different levels
- Abstract Factory: it implements the abstract methods in the Abstract Factory and is responsible for creating specific products
- Abstract Product: compared with the factory method pattern, the products in the abstract factory pattern will be of multiple levels. Therefore, generally, multiple Abstract products will be defined
3. Code example
- Take Haier and TCL as examples to realize the abstract factory model
Create two abstract product classes
-
It is assumed that the product has only two grades: TV and air conditioner
public interface TV { void play(); } public interface AirConditioner { void heat(); }
Create specific product classes
-
Implement abstract product interfaces and create specific product classes for Haier and TCL
public class HaierTV implements TV{ @Override public void play() { System.out.println("Haier TV sets, the sales volume is leading in the country"); } } public class TCLTV implements TV{ @Override public void play() { System.out.println("TCL Brand TV, TV that everyone is watching"); } } public class HaierAirConditioner implements AirConditioner{ @Override public void heat() { System.out.println("Haier brand air conditioner has good heating effect and saves power and worry~"); } } public class TCLAirConditioner implements AirConditioner { @Override public void heat() { System.out.println("TCL Brand air conditioner, heating effect"); } }
Create abstract factory class
-
There are two interfaces for creating a TV and an air conditioner
public interface AbstractFactory { TV getTV(); AirConditioner getAirConditioner(); }
Create a specific factory class
- Create specific factories for Haier and TCL to create specific products
public class HaierFactory implements AbstractFactory{ @Override public TV getTV() { return new HaierTV(); } @Override public AirConditioner getAirConditioner() { return new HaierAirConditioner(); } } public class TCLFactory implements AbstractFactory{ @Override public TV getTV() { return new TCLTV(); } @Override public AirConditioner getAirConditioner() { return new TCLAirConditioner(); } }
4. Summary
- Creating windows components belonging to different operating systems is the earliest application of abstract factory pattern.
- For example, the local implementation of components such as Button and Text in AWT of Java is different in Windows and UNIX.
Applicable scenarios:
- The objects to be created are a series of related or interdependent product families
- Different product families need to be used in different environments (there are multiple product families in the system, but only one of them is used at a time)
Inclination of opening and closing principle
- To add a product family, you only need to add a new factory class without modifying the existing factory - meet the opening and closing principle
- Adding a new level of products, both abstract factories and specific factories need to be modified - they do not meet the opening and closing principle
- This phenomenon is called the inclination of the opening and closing principle
Reference link: These are two different blogs with similar names 😂