当前位置:网站首页>工厂方法模式(Factory Pattern)
工厂方法模式(Factory Pattern)
2022-06-21 05:09:00 【小风的笔记】
工厂方法模式(定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行)
工厂方法模式是对简单工厂模式的进一步抽象化,可以使系统在不修改原来代码的情况下引进新的产品,满足开闭原则。
优点:
1、用户只需要知道具体工厂的名称就可得到所要的产品,无须知道产品的具体创建过程。
2、扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。
3、典型的解耦框架。高层模块只需要知道产品的抽象类,无须关心其他实现类,满足迪米特法则、依赖倒置原则和里氏替换原则。缺点:
1、每次增加一个产品时,都需要增加一个具体产品类和具体工厂类,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度
2、抽象产品只能生产一种产品,此弊端可使用抽象工厂模式解决例子:以工厂生产华为手机和苹果手机为例,类图如下:

工厂方法模式的主要角色如下
抽象工厂(Abstract Factory):提供创建实体对象的接口。
具体工厂(Concrete Factory):实现抽象工厂中创建实体对象的接口。
抽象产品(Abstract Product):为具体产品提供公共接口。
具体产品(Concrete Product):实现抽象产品所定义的接口,由具体工厂来创建,和具体工厂相对应。代码实现如下
抽象工厂
/** * 抽象工厂 */
public interface Factory {
// 生产手机
Phone produce();
}
具体工厂
/** * 华为工厂 */
public class HuaweiFactory implements Factory {
@Override
public Phone produce() {
System.out.println("新华为手机");
return new HuaweiPhone();
}
}
/** * 苹果工厂 */
public class AppleFactory implements Factory {
@Override
public Phone produce() {
System.out.println("新苹果手机");
return new ApplePhone();
}
}
抽象产品
/** * 手机 */
public interface Phone {
// 打电话
void call();
}
具体产品
/** * 华为手机 */
public class HuaweiPhone implements Phone {
@Override
public void call() {
System.out.println("用华为手机打电话...");
}
}
/** * 苹果手机 */
public class ApplePhone implements Phone {
@Override
public void call() {
System.out.println("用苹果手机打电话...");
}
}
测试
public class Test {
public static void main(String[] args) {
Factory huaweiFactory = new HuaweiFactory();
Phone huaweiPhone = huaweiFactory.produce();
huaweiPhone.call();
Factory appleFactory = new AppleFactory();
Phone applePhone = appleFactory.produce();
applePhone.call();
}
}
// 运行结果
新华为手机
用华为手机打电话...
新苹果手机
用苹果手机打电话...
边栏推荐
- golang正则regexp包使用-03-查找匹配字串,查找匹配字串位置,正则分组(FindSubMatch系列方法,FindSubmatchIndex系列方法)
- Station B university learning C language -c language core questions
- [data warehouse modeling] what is the difference between traditional modeling and wide table modeling? Wide table based modeling practice
- 518. 零钱兑换 II
- 常见的请求方式和请求头参数
- Selenium principle application - simulate selenium driven browser with requests
- Idea debug occurs: skipped breakpoint at because it happened inside debugger evaluation
- Launcher page cut Animation
- Seata四大模式之AT模式详解及代码实现
- Steam教育学科融合AR的智能场景
猜你喜欢

JVM performance tuning - 04jvm generational model

Flower sales system based on jsp+servlet+mysql

Alien war source code
![Z 字变形[规律的形式之一 -> 周期考察]](/img/84/0f128a4f8e25a0f8bdcd042625a67e.png)
Z 字变形[规律的形式之一 -> 周期考察]

De duplication according to an attribute in the class

Cvpr2017/ image translation: image to image translation with conditional adversarial networks

JVM performance tuning - 02memory region in JVM

基于SSM+MySQL+Bootstrap+JSP的服装销售商城系统

IDEA Debug出现:Skipped breakpoint at because it happened inside debugger evaluation

GooseFS 在云端数据湖存储上的降本增效实践
随机推荐
JMeter usage teaching
Yolo series ------ (II) loss analysis
创客教育中的空间设计实物原理
Q & A: problems related to "micro build low code" data source
Volley 的是使用以及接口
怎么样学习pmp知识?
MyBaits-plus
猴子都能上手的PUN扩展(房间密码,房间列表,RTC)
【美式发音】IDEA导航栏解析
Jatpack's lifecycle
Day code 300 lines learning notes day 16
Reflective
The El date picker cannot be greater than the current date and time. The exact time is minutes and seconds
Station B university learning C language -c language core questions
基于SSM+MySQL+Layui的博客论坛系统
带图讲解,深度学习YOLO里面的anchors的进阶理解
根据类中某个属性去重
基于SSM+Bootstrap+MySQL+Thymeleaf的汽车维修管理系统
Kotlin技术 - 史上全网最全的 Lambda书写形式
What's the difference between an ordinary tester and an awesome tester? After these two leaps, you can also