当前位置:网站首页>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
边栏推荐
- NPM private server problem of peanut shell intranet penetration mapping
- Pychart cannot run designer Exe (this application failed to start because no Qt platform plugin could be I appears)
- Application of cow read / write replication mechanism in Linux, redis and file systems
- Yamaha robot splits visual strings
- Work accumulation - problems encountered in using ThreadLocal in web requests
- Five solutions across domains
- Efk Upgrade to clickhouse log Storage Reality
- SQL Server 函数
- 打印数字的位信息
- Static proxy mode
猜你喜欢

Five solutions across domains

Household accounting procedures (First Edition)

Transformer中的Self-Attention以及Multi-Head Self-Attention(MSA)
Explore small program audio and video calls and interactive live broadcast from New Oriental live broadcast

C generic speed

Deeply uncover Ali (ant financial) technical interview process with preliminary preparation and learning direction

Cython入门

Import export simple

Spark source code analysis (I): RDD collection data - partition data allocation

Use the fast proxy to build your own proxy pool (mom doesn't have to worry about IP being blocked anymore)
随机推荐
Message queue - function, performance, operation and maintenance comparison
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
Logstash——Logstash将数据推送至Redis
Class and object learning
Message queue - message transaction management comparison
Handwritten background management framework template (I)
MySQL-05
MySQL-08
302. minimum rectangular BFS with all black pixels
302. 包含全部黑色像素的最小矩形 BFS
温度报警器
String class learning
Tencent WXG internship experience (has offered), I hope it will help you!
Comparison between Prometheus and ZABBIX
Implement the runnable interface
事务与消息语义
技术Leader的思考技巧
numpy.random.choice
解决在win10下cmder无法使用find命令
Lamda expression