当前位置:网站首页>Simple factory and factory method mode
Simple factory and factory method mode
2022-07-03 11:53:00 【Z boo, boo, boo】
List of articles
Factory mode
The function is to realize the separation of creator and caller
Core essence : Instantiated objects do not use new, Replace with factory method
Implementation class will be selected , Create object unified management and control . Thus decouple the caller from our implementation class .
The original way of creating objects
Vehicle interface
public interface Car {
void name();
}
Examples of two cars
- Wuling macro light
public class WuLing implements Car {
@Override
public void name() {
System.out.println(" Wuling shenche !");
}
}
- tesla
public class Tesla implements Car{
@Override
public void name() {
System.out.println(" tesla !");
}
}
Consumers build cars
public class Consumer {
//1. The traditional way of creating objects
public static void main(String[] args) {
Car car1 = new WuLing();
Car car2 = new Tesla();
car1.name();
car2.name();
}
}
Originally this way , The caller needs to pass by himself new Keyword to create objects , The disadvantage of this is that we should pay attention to the specific implementation process of an instance , If there are many instantiation parameters of an object , We also need to add these parameters ourselves
Create objects in a simple factory
public class Consumer {
public static void main(String[] args) {
//2. Create objects using factories
Car car1 = CarFactory.getCar(" Wuling ");
Car car2 = CarFactory.getCar(" tesla ");
car1.name();
car2.name();
}
}
The benefits of using factories to create objects are , We only need to pass in a parameter specified by the factory , As for the specific details of object creation , There is no need for our callers to care specifically .
- The above factory mode belongs to the simple factory mode , It's also called static factory mode , In fact, he also has a problem that he is not satisfied OOP The opening and closing principle in the principle , In short, it is , When we add another car , We need to modify the code in the factory directly , In our code, we need to add a else if Statement branch . This way of directly modifying the source code does not meet our opening and closing principles
Create objects using method factories
Car factory
public interface CarFactory {
Car getCar();
}
Create a separate engineering class for each entity, and then implement the car factory
- 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();
}
}
Consumers get instances by creating corresponding factories
public class Consumer {
public static void main(String[] args) {
Car car1 = new TeslaFactory().getCar();
Car car2 = new WuLingFactory().getCar();
car1.name();
car2.name();
}
}
Through the mode of method factory , When we add new classes, we will not modify the original code , Instead, create a factory of the corresponding class , Will not violate the opening and closing principle
- According to the design principles, we choose the factory method mode
- According to the actual business, we choose the simple factory mode
- Because no matter from the code complexity , Structural complexity , In terms of management complexity, simple factory mode is more concise and convenient .
边栏推荐
- Slam mapping and autonomous navigation simulation based on turnlebot3
- MySQL searches and sorts out common methods according to time
- DS90UB949
- STL tutorial 10 container commonalities and usage scenarios
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- Mmc5603nj geomagnetic sensor (Compass example)
- mysql使用update联表更新的方法
- Duplicate numbers in the array of sword finger offer 03
- Dynamic programming (interval DP)
- libvirt 中体验容器
猜你喜欢
随机推荐
STL教程10-容器共性和使用场景
Uniapp implementation Click to load more
Go语言实现静态服务器
如何将数字字符串转换为整数
鸿蒙第三次培训(项目实训)
MCDF实验1
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
MySQL uses the method of updating linked tables with update
R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
Vulnhub narak
previous permutation lintcode51
Qt OpenGL相机的使用
Mmc5603nj geomagnetic sensor (Compass example)
错排问题 (抽奖,发邮件)
Vulnhub pyexp
phpcms 提示信息頁面跳轉showmessage
Hongmeng fourth training
银泰百货点燃城市“夜经济”
剑指offer专项32-96题做题笔记
OpenGL 着色器使用