The relationship between abstract factory patterns can be explained by the following figure. A super factory has multiple sub factories. Each sub factory can produce different products, but the products belong to different product families
As an example, we use code to implement a factory pattern
There are two product levels of mobile phones and routers, and there are two product families of Huawei and Xiaomi. Through the abstract factory model, you can create factories that produce mobile phones and routers at the same time. One is Xiaomi factory that produces Xiaomi mobile phones and Xiaomi routers, and the other is Huawei factory that produces Huawei mobile phones and Huawei routers.
- Create two interfaces: mobile phone and router
- Create two entity classes of Huawei mobile phone and Xiaomi mobile phone to realize the mobile phone interface
- Create two entity classes, Huawei router and Xiaomi router, to implement the router interface
- Create an abstract factory interface and define two methods with return types of mobile interface and router interface
- Create a Xiaomi factory class to implement the abstract factory interface, and return the newly created Xiaomi mobile phone class and Xiaomi router class in the two methods covered
- Create a Huawei factory class to implement the abstract factory interface, and return the newly created Huawei mobile phone class and Huawei router class in the two methods covered
- In the test category, create Xiaomi and Huawei factories to manufacture their branded mobile phones and routers
Mobile product interface:
public interface PhoneProduct { void open(); //Power on void shutdown(); //Shut down void call(); //phone }
Huawei mobile phone implementation class:
public class HuaweiPhone implements PhoneProduct { @Override public void open() { System.out.println("Huawei mobile phone on"); } @Override public void shutdown() { System.out.println("Huawei mobile phone shutdown"); } @Override public void call() { System.out.println("Huawei mobile phone calls"); } }
Xiaomi mobile phone implementation class:
public class XiaomiPhone implements PhoneProduct { @Override public void open() { System.out.println("Millet phone on"); } @Override public void shutdown() { System.out.println("Xiaomi mobile phone off"); } @Override public void call() { System.out.println("Millet phone call"); } }
Router product interface:
public interface RouterProduct { void open(); //Power on void shutdown(); //Shut down void setWifi(); //Set up WiFi }
Huawei router implementation class:
public class HuaweiRouter implements RouterProduct{ @Override public void open() { System.out.println("Millet router boot"); } @Override public void shutdown() { System.out.println("Millet router shutdown"); } @Override public void setWifi() { System.out.println("Xiaomi road router settings WiFi"); } }
Implementation class of Xiaomi Router:
public class XiaomiRouter implements RouterProduct{ @Override public void open() { System.out.println("Millet router boot"); } @Override public void shutdown() { System.out.println("Millet router shutdown"); } @Override public void setWifi() { System.out.println("Xiaomi road router settings WiFi"); } }
Factory interface:
public interface ProductFactory { PhoneProduct phoneProduct(); //Mobile phone factory RouterProduct routerProduct(); //Router factory }
Huawei factory implementation class:
public class HuaweiFactory implements ProductFactory { @Override public PhoneProduct phoneProduct() { return new HuaweiPhone(); } @Override public RouterProduct routerProduct() { return new HuaweiRouter(); } }
Millet factory implementation class:
public class XiaomiFactory implements ProductFactory { @Override public PhoneProduct phoneProduct() { return new XiaomiPhone(); } @Override public RouterProduct routerProduct() { return new XiaomiRouter(); } }
Test class:
public class Client { public static void main(String[] args) { System.out.println("***********Xiaomi series products***********"); XiaomiFactory xiaomiFactory = new XiaomiFactory(); PhoneProduct phoneProduct = xiaomiFactory.phoneProduct(); phoneProduct.open(); phoneProduct.call(); phoneProduct.shutdown(); RouterProduct routerProduct = xiaomiFactory.routerProduct(); routerProduct.open(); routerProduct.setWifi(); routerProduct.shutdown(); System.out.println("***********Huawei series products***********"); HuaweiFactory huaweiFactory = new HuaweiFactory(); PhoneProduct phoneProduct1 = huaweiFactory.phoneProduct(); phoneProduct1.open(); phoneProduct1.call(); phoneProduct1.shutdown(); RouterProduct routerProduct1 = huaweiFactory.routerProduct(); routerProduct1.open(); routerProduct1.setWifi(); routerProduct1.shutdown(); } }
Test results: