当前位置:网站首页>简单工厂和工厂方法模式
简单工厂和工厂方法模式
2022-07-03 10:58:00 【z啵唧啵唧】
工厂模式
作用是实现了创建者和调用者的分离
核心本质:实例化对象不使用new,用工厂方法替代
将选择实现类,创建对象统一管理和控制.从而将调用者跟我们的实现类解耦合.
原先创建对象的方式
车接口
public interface Car {
void name();
}
两个车的实例
- 五菱宏光
public class WuLing implements Car {
@Override
public void name() {
System.out.println("五菱神车!");
}
}
- 特斯拉
public class Tesla implements Car{
@Override
public void name() {
System.out.println("特斯拉!");
}
}
消费者造车
public class Consumer {
//1.传统的创建对象的方式
public static void main(String[] args) {
Car car1 = new WuLing();
Car car2 = new Tesla();
car1.name();
car2.name();
}
}
原先这种方式,需要调用者自己通过new关键字来进行创建对象,这样做的缺点就是我们要关注一个实例的具体实现过程,假如一个对象的实例化参数特别多,我们也需要自己添加这些参数
使用简单工厂的方式创建对象
public class Consumer {
public static void main(String[] args) {
//2.使用工厂创建对象
Car car1 = CarFactory.getCar("五菱");
Car car2 = CarFactory.getCar("特斯拉");
car1.name();
car2.name();
}
}
使用工厂的方式创建对象的好处是,我们只需要传入工厂规定的一个参数即可,至于对象创建的具体细节,不需要我们调用者再去具体关心.
- 上面这种工厂模式属于是简单工厂模式,也叫做静态工厂模式,他其实也存在一个问题就是说他不满足OOP原则中的开闭原则,简单说就是,当我们再添加一个其他的汽车的时候,我们需要直接修改工厂中的代码,在我们这个代码当中就是说需要添加一个else if语句分支.这种直接修改源代码的方式不满足我们的开闭原则
使用方法工厂创建对象
车工厂
public interface CarFactory {
Car getCar();
}
为每一个实体单独创建一个工程类然后实现车工厂
- TeslaFactory
public class TeslaFactory implements CarFactory{
@Override
public Car getCar() {
return new Tesla();
}
}
- WuLingFactory
public class WuLingFactory implements CarFactory{
@Override
public Car getCar() {
return new WuLing();
}
}
消费者通过创建对应的工厂来获取实例
public class Consumer {
public static void main(String[] args) {
Car car1 = new TeslaFactory().getCar();
Car car2 = new WuLingFactory().getCar();
car1.name();
car2.name();
}
}
通过方法工厂的模式,我们在添加新的类的时候就不会在原先的代码上进行修改,而是创建对应的类的工厂即可,不会违背开闭原则
- 根据设计原则我们选择工厂方法模式
- 根据实际业务我们选择简单工厂模式
- 因为无论是从代码复杂度,结构复杂度,管理复杂度来说简单工厂模式都更为简洁和方便.
边栏推荐
- How to become a senior digital IC Design Engineer (1-4) Verilog coding syntax: expression
- The excel table is transferred to word, and the table does not exceed the edge paper range
- ASP. Net hotel management system
- Analysis of JMM memory model
- Kibana~Kibana的安装和配置
- Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
- mysql使用update联表更新的方法
- Using onvif protocol to operate the device
- [VTK] vtkWindowedSincPolyDataFilter 源码注释解读
- GCC compilation process and dynamic link library and static link library
猜你喜欢
LeetCode 46:全排列
Kibana~Kibana的安装和配置
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
Web security summary
Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
DS90UB949
Solve undefined reference to`__ aeabi_ Uidivmod 'and undefined reference to`__ aeabi_ Uidiv 'error
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
Spl06-007 air pressure sensor (example of barometer)
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
随机推荐
How to: configure ClickOnce trust prompt behavior
Numpy np. Max and np Maximum implements the relu function
剑指offer专项32-96题做题笔记
Redis things
银泰百货点燃城市“夜经济”
Analysis of EPS electric steering system
How to become a senior digital IC Design Engineer (1-3) Verilog coding syntax: Verilog behavior level, register transfer level, gate level (abstract level)
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
Leetcode 46: full arrangement
CSRF
[vtk] source code interpretation of vtkpolydatatoimagestencil
Internet socket (non) blocking write/read n bytes
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
Kubernetes 三打探针及探针方式
FL Studio 20 unlimited trial fruit arranger Download
Reading notes: heart like Bodhi, Cao Dewang
uniapp实现点击加载更多
Intel 13th generation core flagship exposure, single core 5.5ghz
Bi skills - permission axis
Illustrated network: what is virtual router redundancy protocol VRRP?