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

Household accounting procedures (First Edition)

【Spark】Spark SQL 字段血缘如何实现

技术Leader的思考技巧

卷妹带你学jdbc---2天冲刺Day2

Logstash - logstash pushes data to redis

DPDK——TCP/UDP协议栈服务端实现(二)

消息队列-功能、性能、运维对比

The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level

EFK昇級到ClickHouse的日志存儲實戰

canal部署、原理和使用介绍
随机推荐
Household accounting procedures (the second edition includes a cycle)
Level signal and differential signal
Logstash - logstash sends an alarm email to email
温度报警器
Implementation of third-party wechat authorized login for applet
Library management system
Day4 branch and loop
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
Definition of Halcon hand eye calibration
Given two corresponding point sets AB, how to estimate the parameters of the specified transformation matrix R?
消息队列-全方位对比
Household accounting procedures (First Edition)
【Spark】Spark SQL 字段血缘如何实现
Yamaha robot splits visual strings
The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level
302. 包含全部黑色像素的最小矩形 BFS
Evolution history of qunar Bi platform construction
SQL server functions
Logstash——Logstash向Email发送告警邮件
[spark] how to implement spark SQL field blood relationship