当前位置:网站首页>简单工厂和工厂方法模式
简单工厂和工厂方法模式
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();
}
}
通过方法工厂的模式,我们在添加新的类的时候就不会在原先的代码上进行修改,而是创建对应的类的工厂即可,不会违背开闭原则
- 根据设计原则我们选择工厂方法模式
- 根据实际业务我们选择简单工厂模式
- 因为无论是从代码复杂度,结构复杂度,管理复杂度来说简单工厂模式都更为简洁和方便.
边栏推荐
- GCC compilation process and dynamic link library and static link library
- C language log base zlog basic use
- uniapp实现点击加载更多
- Qt+VTK+OCCT读取IGES/STEP模型
- How to get started embedded future development direction of embedded
- PHP server interacts with redis with a large number of close_ Wait analysis
- How to become a senior digital IC Design Engineer (1-4) Verilog coding syntax: expression
- STL教程8-map
- How to become a senior digital IC Design Engineer (1-5) Verilog coding syntax: operand
- 软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
猜你喜欢

DS90UB949

836. 合并集合(DAY 63)并查集

Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution

Kubernetes 三打探针及探针方式

Qt+VTK+OCCT读取IGES/STEP模型

Mmc5603nj geomagnetic sensor (Compass example)

Analysis of EPS electric steering system

Matlab extracts numerical data from irregular txt files (simple and practical)

Redis things

FL Studio 20 unlimited trial fruit arranger Download
随机推荐
Viewing binary bin files with notepad++ editor
Program process management tool -go Supervisor
2022 东北四省赛 VP记录/补题
mysql使用update联表更新的方法
FL Studio 20 unlimited trial fruit arranger Download
Phpcms prompt message page Jump to showmessage
DS90UB949
ASP.NET-酒店管理系统
Kibana~Kibana的安装和配置
The excel table is transferred to word, and the table does not exceed the edge paper range
Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
Qt+VTK+OCCT读取IGES/STEP模型
Excel quick cross table copy and paste
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
Mysql根据时间搜索常用方法整理
Redis things
Some common terms
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
C language two-dimensional array
ASP.NET-酒店管理系統