当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022

Vulnhub geminiinc

【mysql官方文档】死锁

Numpy np. Max and np Maximum implements the relu function

PHP Basics

Ripper of vulnhub

Xml的(DTD,xml解析,xml建模)

Momentum of vulnhub

Hongmeng fourth training

Slam mapping and autonomous navigation simulation based on turnlebot3
随机推荐
Introduction to the implementation principle of rxjs observable filter operator
《剑指offer 04》二维数组查找
Vulnhub geminiinc
OpenStack中的测试分类
Kibana~Kibana的安装和配置
Go语言实现静态服务器
Notes on 32-96 questions of sword finger offer
Hongmeng fourth training
cgroup简介
Understand go language context in one article
外插散点数据
OpenGL 着色器使用
简单工厂和工厂方法模式
同事写了一个责任链模式,bug无数...
Web security summary
VS2015的下载地址和安装教程
typeScript
Raven2 of vulnhub
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
Qt OpenGL 旋转、平移、缩放