当前位置:网站首页>Gof23 - abstract factory pattern
Gof23 - abstract factory pattern
2022-06-26 06:14:00 【Kuxiaoya】
Let's talk about this Abstract factory pattern !
Last article ( Click to see ):GoF23— Factory mode
review ( Three models ) :
Simple factory model (Simple Factory)
Used to produce any product in a unified hierarchical structure ( For adding new products , need Modify the original code , Although the amount of code is small , Poor maintenance !)
Although it does not conform to the design principles to some extent , But actually Most used !
Factory method model (Factory Method)
Used to produce fixed products in the same hierarchy ( Support to add any product , Large amount of code , But the maintainability is good !)
Horizontal scaling , Does not affect operation !
Abstract factory pattern (Abstract Factory)
Around a Super factory Create other factories . This super factory is also called other The factory of the factory .
Do not add products , Product families can be added , It is very difficult to expand new products !
Abstract factory pattern :

package factory.abstract1;
// Mobile phone product interface
public interface PhoneProduct {
void start();
void music();
void code();
void shutdown();
}
package factory.abstract1;
// Router product interface
public interface RouterProduct {
void start();
void openWife();
void watchTv();
void shutdown();
}
package factory.abstract1;
// Abstract product factory
public interface ProductFactory {
// Production of mobile phone
PhoneProduct phoneProduct();
// Making routers
RouterProduct routerProduct();
}
package factory.abstract1;
// Mi phones
public class XiaoMiPhone implements PhoneProduct{
@Override
public void start() {
System.out.println(" Turn on Xiaomi mobile phone ");
}
@Override
public void music() {
System.out.println(" Listen to music with Xiaomi mobile phone ");
}
@Override
public void code() {
System.out.println(" Use Xiaomi's mobile phone to type the code ");
}
@Override
public void shutdown() {
System.out.println(" Turn off Xiaomi's mobile phone ");
}
}
package factory.abstract1;
// Huawei mobile phones
public class HuaWeiPhone implements PhoneProduct{
@Override
public void start() {
System.out.println(" Turn on Huawei mobile phone ");
}
@Override
public void music() {
System.out.println(" Listen to music on Huawei mobile phones ");
}
@Override
public void code() {
System.out.println(" Use the Huawei mobile phone to type the code ");
}
@Override
public void shutdown() {
System.out.println(" Turn off Huawei mobile phone ");
}
}
package factory.abstract1;
// Xiaomi router
public class XiaoMiRouter implements RouterProduct{
@Override
public void start() {
System.out.println(" Turn on Xiaomi router ");
}
@Override
public void openWife() {
System.out.println(" Use the Xiaomi router to open wife");
}
@Override
public void watchTv() {
System.out.println(" Use the Xiaomi router to watch TV ");
}
@Override
public void shutdown() {
System.out.println(" Turn off Xiaomi router ");
}
}
package factory.abstract1;
// Huawei router
public class HuaWeiRouter implements RouterProduct{
@Override
public void start() {
System.out.println(" Turn on Huawei router ");
}
@Override
public void openWife() {
System.out.println(" Use Huawei router to open Wife");
}
@Override
public void watchTv() {
System.out.println(" Use Huawei router to watch TV ");
}
@Override
public void shutdown() {
System.out.println(" Turn off Huawei router ");
}
}
package factory.abstract1;
// Millet factory
public class XiaoMiFactory implements ProductFactory{
@Override
public PhoneProduct phoneProduct() {
return new XiaoMiPhone();
}
@Override
public RouterProduct routerProduct() {
return new XiaoMiRouter();
}
@Override
public Laptop laptop() {
return new XiaoMiLaptop();
}
}
package factory.abstract1;
// Huawei factory
public class HuaWeiFactory implements ProductFactory{
@Override
public PhoneProduct phoneProduct() {
return new HuaWeiPhone();
}
@Override
public RouterProduct routerProduct() {
return new HuaWeiRouter();
}
@Override
public Laptop laptop() {
return new HuaWeiLaptop();
}
}
package factory.abstract1;
// client
public class Client {
public static void main(String[] args) {
System.out.println("------------------ Millet series products ------------------");
// Millet factory
XiaoMiFactory xiaoMiFactory = new XiaoMiFactory();
PhoneProduct phoneProduct = xiaoMiFactory.phoneProduct();
phoneProduct.music();
phoneProduct.code();
XiaoMiRouter xiaoMiRouter = new XiaoMiRouter();
xiaoMiRouter.openWife();
xiaoMiRouter.watchTv();
System.out.println("------------------ Huawei series products ------------------");
HuaWeiFactory huaWeiFactory = new HuaWeiFactory();
// Mobile phone products in Huawei factory
PhoneProduct product = huaWeiFactory.phoneProduct();
product.code();
product.music();
// Router products in Huawei factory
RouterProduct routerProduct = huaWeiFactory.routerProduct();
routerProduct.openWife();
routerProduct.watchTv();
}
}

边栏推荐
- The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level
- 花生壳内网穿透映射NPM私服问题
- Message queuing - omnidirectional comparison
- MySQL 索引底层原理
- Prometheus和Zabbix的对比
- Underlying principle of MySQL index
- numpy.random.choice
- 工作积累——Web请求中使用ThreadLocal遇见的问题
- Implementation of third-party wechat authorized login for applet
- numpy. frombuffer()
猜你喜欢

MySQL 索引底层原理

卷妹带你学jdbc---2天冲刺Day2

Tencent WXG internship experience (has offered), I hope it will help you!

Household accounting procedures (the second edition includes a cycle)

Test depends on abstraction and does not depend on concrete

Message queue - function, performance, operation and maintenance comparison

实时数仓方案如何选型和构建

Import / export function implementation

Logstash - logstash pushes data to redis

TCP连接与断开,状态迁移图详解
随机推荐
Hot! 11 popular open source Devops tools in 2021!
MySQL-05
04. basic data type - list, tuple
MySQL-08
数据可视化实战:数据可视化
Comparison between Prometheus and ZABBIX
tf.nn.top_k()
Message queuing - omnidirectional comparison
Thinking and summary of technical ability
MVC source code sharing
如何设计好的技术方案
Redis multithreading and ACL
302. 包含全部黑色像素的最小矩形 BFS
volatile应用场景
Architecture design method
05. basic data type - Dict
Keepalived to achieve high service availability
【群内问题学期汇总】初学者的部分参考问题
如何让主线程等待子线程执行完毕后再执行
消息队列-消息事务管理对比