当前位置:网站首页>Gof23 - factory mode
Gof23 - factory mode
2022-06-26 06:14:00 【Kuxiaoya】
What is factory mode :
Factory mode (Factory Pattern) yes Java One of the most common design patterns in . This type of design pattern is a creation pattern , It provides the best way to create objects .
In factory mode , We don't expose the creation logic to the client when we create the object , And by using a common interface to point to newly created objects .
The core essence of the factory model :
- Instantiated object is not applicable new, Replace with factory method
- Implementation class will be selected , Create object unified management and control . To decouple the caller from our implementation class .
Three models :
Simple factory model (Simple Factory)
- Used to produce... In a unified hierarchical structure
Any product( For adding new products , needModify the original code, Although the amount of code is small , Poor maintenance !) - Although it does not conform to the design principles to some extent , But actually
Most used!
Factory method model (Factory Method)
- Used to produce... In the same hierarchical structure
Fixed products(Support to add any product, Large amount of code , But the maintainability is good !) Horizontal scaling, Does not affect operation !
Abstract factory pattern (Abstract Factory)
- Around a
Super factoryCreate other factories . This super factory is also called otherThe factory of the factory. - Do not add products , Sure
Add product family!
Simple factory model : ( Also called static factory mode )

package factory.simple;
// Interface
public interface Car {
void name();
}
package factory.simple;
public class DaZhong implements Car{
@Override
public void name() {
System.out.println(" The public ");
}
}
package factory.simple;
public class Tesla implements Car{
@Override
public void name() {
System.out.println(" tesla ");
}
}
package factory.simple;
public class WuLing implements Car{
@Override
public void name() {
System.out.println(" Wuling macro light !");
}
}
package factory.simple;
/** Static factory mode ( Simple factory model ) * shortcoming : Add a new product , If you don't change the code , Can't do ! * Failure to meet the opening and closing principle */
public class CarFactory {
// Method 1
// public static Car getCar(String car){
// if (car.equals(" Wuling ")){
// return new WuLing();
// }else if(car.equals("Tesla")){
// return new Tesla();
// }else{
// return null;
// }
// }
// Method 2
public static Car getWuLing(){
return new WuLing();
}
public static Car getTesla(){
return new Tesla();
}
public static Car getDaZhong(){
return new DaZhong();
}
}
package factory.simple;
// All implementation classes of the interface !
public class Consumer {
public static void main(String[] args) {
// Method 1
// Car car = CarFactory.getCar(" Wuling ");
// car.name();
// CarFactory.getCar("Tesla").name();
// Method 2
CarFactory.getTesla().name();
CarFactory.getWuLing().name();
CarFactory.getDaZhong().name();
}
}

Factory method model :

package factory.method;
public interface Car {
void name();
}
package factory.method;
// Factory method model
public interface CarFactory {
Car getCar();
}
package factory.method;
public class WuLing implements Car {
@Override
public void name() {
System.out.println(" Wuling macro light !");
}
}
package factory.method;
public class WuLingFactory implements CarFactory{
@Override
public Car getCar() {
return new WuLing();
}
}
package factory.method;
public class Tesla implements Car {
@Override
public void name() {
System.out.println(" tesla ");
}
}
package factory.method;
public class TeslaFactory implements CarFactory{
@Override
public Car getCar() {
return new Tesla();
}
}
package factory.method;
public class MoBai implements Car{
@Override
public void name() {
System.out.println(" The worship the bike ");
}
}
package factory.method;
public class MoBaiFactory implements CarFactory{
@Override
public Car getCar() {
return new MoBai();
}
}
package factory.method;
// All implementation classes of the interface !
public class Consumer {
public static void main(String[] args) {
new WuLingFactory().getCar().name();
new TeslaFactory().getCar().name();
new MoBaiFactory().getCar().name();
}
}

Abstract factory pattern :
Next article ( Click to see ):GoF23— Abstract factory pattern
边栏推荐
- Underlying principle of MySQL index
- tf.nn.top_k()
- Implementation of third-party wechat authorized login for applet
- 温度报警器
- Household accounting procedures (First Edition)
- canal部署、原理和使用介绍
- 数据治理工作的几种推进套路
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
- Cython入门
- numpy. random. choice
猜你喜欢

技术Leader的思考技巧

如何设计好的技术方案

Detailed explanation of serial port communication principle 232, 422, 485

Vs2022 offline installation package download and activation

低代码实时数仓构建系统的设计与实践

Comparison between Prometheus and ZABBIX

ByteDance starts the employee's sudden wealth plan and buys back options with a large amount of money. Some people can earn up to 175%

MVC source code sharing
Explore small program audio and video calls and interactive live broadcast from New Oriental live broadcast

DS18B20详解
随机推荐
University Information Management System
MySQL-07
消息队列-消息事务管理对比
COW读写复制机制在Linux,Redis ,文件系统中的应用
TCP连接与断开,状态迁移图详解
Pychart cannot run designer Exe (this application failed to start because no Qt platform plugin could be I appears)
Matching environment of ES6
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
Work accumulation - problems encountered in using ThreadLocal in web requests
Static proxy mode
Typora activation method
Implement the runnable interface
The difference between abstract and interface interface
Adapter mode
DS18B20详解
工作积累——Web请求中使用ThreadLocal遇见的问题
Basic construction of SSM framework
Interface oriented programming
MySQL 索引底层原理
事务与消息语义