当前位置:网站首页>[creation mode] factory method mode
[creation mode] factory method mode
2022-06-11 15:23:00 【Evader1997】
Review the simple factory model
The last column introduced the simple factory mode , its UML The class diagram looks like this 
In the simple factory pattern, the factory class is used to create four corresponding to addition, subtraction, multiplication and division ‘ product ’, At this point, we create the object in the factory class through switch Of case To decide . If the computer needs to add a residual method , How do we do it ?
- Create a new remainder operation class to inherit the parent operation class Operation
- In factory class OperationFactory Of switch One more in case sentence ( You need to modify the code of this class )
Creating a new residual class will not affect , however modify OperationFactory The code in the class violates the open closed principle , And every time a new algorithm class is added, the code in the factory class needs to be modified ! So there is a factory approach !
I understand the factory method
Let's first look at the factory method UML Class diagram 
Compare the two pictures , It is clear that the factory method will One factory has become four factories , Every factory is very specific ,FactoryAdd The factory only produces addition objects ,FactoryDiv The factory only produces division objects ,FactoryMul Only multiplicative objects are produced ,FactorySub Only subtraction objects are produced .
The factory method is an extension of the simple factory , From the review, we can see that the disadvantage of a simple factory is that it is not conducive to expansion . The factory approach perfectly addresses this shortcoming , It further abstracts the factory in the simple factory pattern , It has become a universal factory , This universal factory only provides methods for producing objects , The permission to create concrete objects is given to the implementation class .
The factory method pattern implements the calculator
Factory method interface
public interface FactoryMethod {
Operation createOperation();
}
Factory classes that implement factory methods
public class FactoryAdd implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationAdd();
}
}
public class FactoryDiv implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationDiv();
}
}
public class FactoryMul implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationMul();
}
}
public class FactorySub implements FactoryMethod {
@Override
public Operation createOperation() {
return new OperationSub();
}
}
test
public class MainTest {
public static void main(String[] args) {
FactoryMethod factoryMethod = new FactorySub();
Operation operation = factoryMethod.createOperation();
operation.setNum1(2);
operation.setNum2(3);
System.out.println(" The result is :" + operation.getResult());
}
}
Running results :
The result is :-1
summary
The factory method pattern uses polymorphism , So it retains the advantages of simple factory , Customer service has overcome its shortcomings , A residual function is added in the factory method mode , There is no need to modify the existing code , You only need to add a factory class of the production surplus object , But this time, some development will be added ( A few more lines of code for a simple factory , Factory methods need to add a class ).
If you don't know why you use the simple factory, please see the simple factory article in the column , Hard core analysis !
Simple factory model hard core analysis
边栏推荐
- How to do well in we media? Did you do these steps right?
- 02 _ 日志系统:一条SQL更新语句是如何执行的?
- 思科瑞递交科创板注册:拟募资6亿 年营收2.22亿
- 见微知著,细节上雕花:SVG生成矢量格式网站图标(Favicon)探究
- [verification of SystemVerilog] ~ test platform, hardware design description, excitation generator, monitor and comparator
- [mysql_12] MySQL data types
- Intercept string (function)
- Hamad application layout scheme 05 of hashicopy (visit the web page)
- Hebei huangjinzhai scenic spot adds "AED automatic defibrillator" to ensure the life safety of tourists!
- With a loss of 13.6 billion yuan in three years, can listing revive Weima?
猜你喜欢
随机推荐
Turning "passive" into "active", how to build security compliant intelligent products | Q recommendation
Hashicopy之nomad应用编排方案01
In the "ten billion blue ocean" database, each player can find a boat | c-position face-to-face
Social software soul withdraws its IPO application: Tencent is a major shareholder
Analysis on the architecture of distributed systems - transaction and isolation level (multi object, multi operation) Part 2
Database optimization
04 _ In simple terms index (I)
汤峥嵘:CTO 是商业思维和技术思维交汇的那个点
Flutter 3.0 was officially released: it stably supports 6 platforms, and byte jitter is the main user
Microservices - use of Nacos
[SystemVerilog interface] ~ interface
How to batch insert 100000 pieces of data
Why can redis be so fast?
Backtracking / solution space tree permutation tree
The reason why it is easy to let the single chip computer program fly
Interview shock 26: how to stop threads correctly?
企业开发如何写出优雅的二级分类【美团小案例】
06 _ 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology
Hot seek tiger, a list of eco economic models








