当前位置:网站首页>简单工厂和工厂方法模式
简单工厂和工厂方法模式
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();
}
}
通过方法工厂的模式,我们在添加新的类的时候就不会在原先的代码上进行修改,而是创建对应的类的工厂即可,不会违背开闭原则
- 根据设计原则我们选择工厂方法模式
- 根据实际业务我们选择简单工厂模式
- 因为无论是从代码复杂度,结构复杂度,管理复杂度来说简单工厂模式都更为简洁和方便.
边栏推荐
- R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
- R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
- Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
- uniapp实现点击加载更多
- 程序员的创业陷阱:接私活
- Machine learning 3.2 decision tree model learning notes (to be supplemented)
- 动态规划(区间dp)
- 基于I2C协议的驱动开发
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- AOSP ~ NTP ( 网络时间协议 )
猜你喜欢
Processes and threads
Understand go language context in one article
Excel快速跨表复制粘贴
银泰百货点燃城市“夜经济”
The world's most popular font editor FontCreator tool
How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
鸿蒙第四次培训
ASP. Net hotel management system
After a month, I finally got Kingdee offer! Share tetrahedral Sutra + review materials
抓包整理外篇fiddler———— 会话栏与过滤器[二]
随机推荐
Project management essence reading notes (VII)
Oracle 11g single machine cold standby database
DS90UB949
Execute kubectl on Tencent cloud container service node
The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
After setting up ADG, instance 2 cannot start ora-29760: instance_ number parameter not specified
asyncio 警告 DeprecationWarning: There is no current event loop
R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
Spl06-007 air pressure sensor (example of barometer)
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
Excel quick cross table copy and paste
Oracle withdraw permission & create role
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
2022年中南大学夏令营面试经验
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
按键切换:按F1-F12都需要按Fn
repo ~ 常用命令
Numpy np.max和np.maximum实现relu函数
Function details of CorelDRAW graphics suite 2022
R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA