当前位置:网站首页>简单工厂和工厂方法模式
简单工厂和工厂方法模式
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();
}
}
通过方法工厂的模式,我们在添加新的类的时候就不会在原先的代码上进行修改,而是创建对应的类的工厂即可,不会违背开闭原则
- 根据设计原则我们选择工厂方法模式
- 根据实际业务我们选择简单工厂模式
- 因为无论是从代码复杂度,结构复杂度,管理复杂度来说简单工厂模式都更为简洁和方便.
边栏推荐
- [vtk] source code interpretation of vtkpolydatatoimagestencil
- uniapp实现点击加载更多
- ASP. Net hotel management system
- LeetCode 46:全排列
- Matlab extracts numerical data from irregular txt files (simple and practical)
- 836. 合并集合(DAY 63)并查集
- 基于I2C协议的驱动开发
- Processes and threads
- C language AES encryption and decryption
- R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
猜你喜欢

Understand go language context in one article

The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.

MATLAB提取不规则txt文件中的数值数据(简单且实用)

2022 东北四省赛 VP记录/补题

AOSP ~ NTP ( 网络时间协议 )

How to clean up v$rman_ backup_ job_ Details view reports error ora-02030

ASP.NET-酒店管理系统

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?

Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests

Excel quick cross table copy and paste
随机推荐
Understand go language context in one article
Kubernetes 三打探针及探针方式
Tablespace creation management and control file management
Function details of CorelDRAW graphics suite 2022
[vtk] source code interpretation of vtkpolydatatoimagestencil
LeetCode 46:全排列
聊聊Flink框架中的状态管理机制
phpcms 提示信息頁面跳轉showmessage
ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
Some common terms
[VTK] vtkPolydataToImageStencil 源码解读
How to get started embedded future development direction of embedded
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
2022 东北四省赛 VP记录/补题
How to become a senior digital IC Design Engineer (1-5) Verilog coding syntax: operand
R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
Qt+VTK+OCCT读取IGES/STEP模型
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
银泰百货点燃城市“夜经济”
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.