Abstract factory pattern
Factories that create factories
It feels like another layer of encapsulation on the method factory pattern,
Create other factories around one super factory. The super factory is also known as the factory of other factories
definition:
The abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their specific classes (for the whole product family, the number of product grades is relatively fixed)
Applicable scenarios:
- The client (application layer) does not depend on the details of how product class instances are created and implemented
- Emphasize that a series of related product objects (belonging to the same product family) are used together, and creating objects requires a lot of repetitive code
- Provide a library of product classes. All products appear with the same interface, so that the client does not depend on the specific implementation
Product concept
- For example, Xiaomi mobile phone, Xiaomi router, Xiaomi TV and Xiaomi refrigerator belong to the same Xiaomi product family
- Xiaomi mobile phones belong to the same product level as Huawei mobile phones, both of which are mobile phones

// Mobile product interface public interface IphoneProduct { void start(); void shutdown(); void callup(); void sendSMS(); } // Mi phones public class XiaomiPhone implements IphoneProduct{ @Override public void start() { System.out.println("Turn on Xiaomi mobile phone"); } @Override public void shutdown() { System.out.println("Turn off Xiaomi mobile phone"); } @Override public void callup() { System.out.println("Millet phone call"); } @Override public void sendSMS() { System.out.println("Xiaomi texting"); } } // Huawei mobile phone public class HuaweiPhone implements IphoneProduct{ @Override public void start() { System.out.println("Turn on Huawei mobile phone"); } @Override public void shutdown() { System.out.println("Turn off Huawei mobile phone"); } @Override public void callup() { System.out.println("Huawei mobile phone calls"); } @Override public void sendSMS() { System.out.println("Huawei SMS"); } } // Router product interface public interface IRouterProduct { void start(); void shutdown(); void openWifi(); void setting(); } // Xiaomi router public class XiaomiRouter implements IRouterProduct{ @Override public void start() { System.out.println("Start Xiaomi router"); } @Override public void shutdown() { System.out.println("Turn off Xiaomi router"); } @Override public void openWifi() { System.out.println("Open Xiaomi Wi-Fi"); } @Override public void setting() { System.out.println("Millet settings"); } } // Huawei router public class HuaweiRouter implements IRouterProduct{ @Override public void start() { System.out.println("Start Huawei router"); } @Override public void shutdown() { System.out.println("Turn off Huawei router"); } @Override public void openWifi() { System.out.println("Open Huawei Wi-Fi"); } @Override public void setting() { System.out.println("Huawei settings"); } } // Abstract product factory public interface IProductFactory { // Production of mobile phones IphoneProduct iphoneProduct(); // Production router IRouterProduct irouterProduct(); } public class XiaomiFactory implements IProductFactory{ @Override public IphoneProduct iphoneProduct() { return new XiaomiPhone(); } @Override public IRouterProduct irouterProduct() { return new XiaomiRouter(); } } public class HuaweiFactory implements IProductFactory{ @Override public IphoneProduct iphoneProduct() { return new HuaweiPhone(); } @Override public IRouterProduct irouterProduct() { return new HuaweiRouter(); } } public class Client { public static void main(String[] args) { System.out.println("Xiaomi series products--------------------"); // Millet factory XiaomiFactory xiaomiFactory = new XiaomiFactory(); IphoneProduct iphoneProduct = xiaomiFactory.iphoneProduct(); iphoneProduct.callup(); iphoneProduct.sendSMS(); IRouterProduct iRouterProduct = xiaomiFactory.irouterProduct(); iRouterProduct.openWifi(); System.out.println("Huawei series products--------------------"); // Millet factory HuaweiFactory huaweiFactory = new HuaweiFactory(); iphoneProduct = huaweiFactory.iphoneProduct(); iphoneProduct.callup(); iphoneProduct.sendSMS(); iRouterProduct = huaweiFactory.irouterProduct(); iRouterProduct.openWifi(); } }
advantage
The code isolation of specific products in the application layer does not need to care about the details of creation
Create a series of products together
Disadvantages:
All possible product sets are specified, and it is difficult to expand new products in the product family;
It increases the abstraction and understanding difficulty of the system
Summary of plant mode:
- Simple factory mode: although it does not conform to the design principles to some extent, it is actually used most
- Factory method mode: extend by adding new factory classes without modifying existing classes
- Abstract factory mode: products can not be added, but product families can be added
Application scenario:
- getInstance method of calendar in jdk
- Acquisition of Connection object in JDBC
- The IOC container in Spring creates a managed bean object
- newInstance method of Class object in reflection