当前位置:网站首页>Abstract factory pattern
Abstract factory pattern
2022-06-30 08:59:00 【Hujiajun】

The relationship between abstract factory patterns can be explained by the following figure , There are several sub factories under a super factory , Each sub factory can produce different products , But products belong to different product families


Here's an example , We use code to implement a factory pattern
There are two product levels: mobile phones and routers , There are two product families, Huawei and Xiaomi , Through the abstract factory pattern, you can create a factory that produces both mobile phones and routers , One is that Xiaomi factory produces Xiaomi mobile phones and Xiaomi routers , The other is that Huawei factory produces Huawei mobile phones and Huawei routers .
- Create two interfaces of mobile phone and router
- Create two entity classes, Huawei mobile and Xiaomi mobile, to implement the mobile interface
- Create two entity classes, Huawei router and Xiaomi router, to implement the router interface
- Create an abstract factory interface , Define two methods whose return types are mobile interface and router interface
- Create a millet factory class to implement the abstract factory interface , Return the newly created Xiaomi mobile phone class and Xiaomi router class in the two method bodies covered
- Create a Huawei factory class to implement the abstract factory interface , Return the newly created Huawei mobile phone class and Huawei router class in the two method bodies covered
- Create Xiaomi and Huawei factories in the test category to manufacture their branded mobile phones and routers
Mobile phone product interface :
public interface PhoneProduct {
void open(); // Turn it on
void shutdown(); // To turn it off
void call(); // Make a phone call
}
Huawei mobile phone implementation class :
public class HuaweiPhone implements PhoneProduct {
@Override
public void open() {
System.out.println(" Huawei mobile phone starts ");
}
@Override
public void shutdown() {
System.out.println(" Huawei's mobile phone is powered off ");
}
@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(" Xiaomi's mobile phone is on ");
}
@Override
public void shutdown() {
System.out.println(" Xiaomi's cell phone is off ");
}
@Override
public void call() {
System.out.println(" Xiaomi makes a phone call ");
}
}
Router product interface :
public interface RouterProduct {
void open(); // Turn it on
void shutdown(); // To turn it off
void setWifi(); // Set up WiFi
}
Huawei router implementation class :
public class HuaweiRouter implements RouterProduct{
@Override
public void open() {
System.out.println(" Xiaomi router is powered on ");
}
@Override
public void shutdown() {
System.out.println(" Xiaomi router is turned off ");
}
@Override
public void setWifi() {
System.out.println(" Xiaomi road router settings WiFi");
}
}
Xiaomi router implementation class :
public class XiaomiRouter implements RouterProduct{
@Override
public void open() {
System.out.println(" Xiaomi router is powered on ");
}
@Override
public void shutdown() {
System.out.println(" Xiaomi router is turned off ");
}
@Override
public void setWifi() {
System.out.println(" Xiaomi road router settings WiFi");
}
}
Factory interface :
public interface ProductFactory {
PhoneProduct phoneProduct(); // Cell 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("*********** Millet 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 result :

边栏推荐
- Alcohol tester scheme: what principle does the alcohol tester measure alcohol solubility based on?
- 技术管理进阶——管理者如何进行梯队设计及建设
- 14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
- [data analysis and display]
- [paid promotion] collection of frequently asked questions, FAQ of recommended list
- CUDA realizes L2 European distance
- [kotlin collaboration process] complete the advanced kotlin collaboration process
- Gilbert Strang's course notes on linear algebra - Lesson 4
- Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
- Gilbert Strang's course notes on linear algebra - Lesson 3
猜你喜欢

Flink SQL 自定义 Connector

Maxiouassigner of mmdet line by line interpretation

Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)

Pytorch BERT

Does the oscilloscope probe affect the measurement of capacitive load?

Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)

2021-05-06

100 lines of code and a voice conversation assistant

Detailed explanation of pipline of mmdetection

将线程绑定在某个具体的CPU逻辑内核上运行
随机推荐
127.0.0.1, 0.0.0.0 and localhost
[untitled]
C#访问SQL Server数据库两种方式的比较(SqlDataReader vs SqlDataAdapter)
Redis design and Implementation (I) | data structure & object
Codeworks 5 questions per day (1700 for each) - the third day
Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
Redis design and Implementation (IV) | master-slave replication
Dart tips
127.0.0.1、0.0.0.0和localhost
Alcohol tester scheme: what principle does the alcohol tester measure alcohol solubility based on?
Gilbert Strang's course notes on linear algebra - Lesson 1
Detailed explanation of pytoch's scatter function
mysql基础入门 day3 动力节点[老杜]课堂笔记
Is the reverse repurchase of treasury bonds absolutely safe? How to open an account online
Unity simple shader
[untitled]
Flink Exception -- No ExecutorFactory found to execute the application
Occasionally, Flink data is overstocked, resulting in checkpoint failure
100 lines of code and a voice conversation assistant
Design specification for smart speakers v1.0