当前位置:网站首页>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 .
边栏推荐
- 利用Zabbix动态监控磁盘I/O
- R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
- Extrapolated scatter data
- Excel表格转到Word中,表格不超边缘纸张范围
- Yintai department store ignites the city's "night economy"
- Visual Studio 2022下载及配置OpenCV4.5.5
- 简单工厂和工厂方法模式
- MySQL searches and sorts out common methods according to time
- Vulnhub geminiinc V2
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
猜你喜欢

2022 northeast four provinces match VP record / supplementary questions

vulnhub之presidential

Web安全总结

Momentum of vulnhub

Excel quick cross table copy and paste

Vulnhub's Tomato (tomato)

Excel表格转到Word中,表格不超边缘纸张范围

Kibana - installation and configuration of kibana

How to get started embedded future development direction of embedded

DS90UB949
随机推荐
简单工厂和工厂方法模式
Kibana - installation and configuration of kibana
vulnhub之Ripper
Notes on 32-96 questions of sword finger offer
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
cgroup简介
OpenStack中的测试分类
聊聊Flink框架中的状态管理机制
VS2015的下载地址和安装教程
2022年湖南工学院ACM集训第二次周测题解
How should intermediate software designers prepare for the soft test
在CoreOS下部署WordPress实例教程
STL教程8-map
MySQL union和union all区别
rxjs Observable filter Operator 的实现原理介绍
vulnhub之raven2
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
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
ftp登录时,报错“530 Login incorrect.Login failed”
鸿蒙第三次培训(项目实训)