当前位置:网站首页>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 :
边栏推荐
- Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
- Flink 数据偶尔数据积压导致checkpoint失败
- Esp32 (4): overview of the overall code architecture
- Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)
- Flink sql -- No factory implements ‘org.apache.flink.table.delegation.ExecutorFactory‘.
- 酒精测试仪方案:酒精测试仪是根据什么原理测酒精溶度?
- Rew acoustic test (VI): signal and measurement
- [untitled]
- 2021-04-29
- Pytorch BERT
猜你喜欢
[untitled]
Flink SQL custom connector
Pytorch BERT
Rew acoustic test (III): generate test signal
Redis design and Implementation (IV) | master-slave replication
Based on svelte3 X desktop UI component library svelte UI
自制GIF动态图-gifcam
Flink 数据偶尔数据积压导致checkpoint失败
2021-04-29
Tidb 6.0: making Tso more efficient tidb Book rush
随机推荐
Anchorgenerator for mmdet line by line interpretation
Rew acoustic test (I): microphone calibration
Sort (simple description)
Flink Exception -- No ExecutorFactory found to execute the application
How can we get a satisfactory salary? These routines still need to be mastered
[cmake] make command cannot be executed normally
Summary of common pytoch APIs
vim 从嫌弃到依赖(21)——跨文件搜索
Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)
Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
Pytorch BERT
El input limit can only input numbers
Detailed explanation of pipline of mmdetection
Circuit analysis of current probe
Axure make menu bar effect
Esp32 things (3): overview of the overall system design
CUDA implements matrix replication
Build a docker image of Henkel database from 0
挖财开户安全吗?怎么有人说不靠谱。
C accesses mongodb and performs CRUD operations