当前位置:网站首页>Explain the factory method
Explain the factory method
2022-06-09 14:48:00 【Floral Sea】
Explain the factory method in detail
For better explanation Factory method , Let's not give rigid language , Let's look at it from a practical example
If we want to build a Calculator , Define addition 、 Multiplication 、 Various operations such as division , in consideration of encapsulation Thought , We might construct something like an addition class , Multiplication class , Divide the class , A series of classes such as subtraction class are as follows :
- Class OperationAdd
- Class OperationMinus
- Class OperationMul
- Class OperationDiv
When we want to add new functions, we can add corresponding classes , But this brings about a problem , When we want to use methods in a particular class , If not Static class We all need to instantiate an object first , For example, we need to do continuous operations such as multiplication and division , Then our next behavior may be like this :Class mul = new OperationMul()Class div = new OperationDiv()...
You may need the most repeated operations , Our code may become bloated . In fact, what we need to consider at this time is how to instantiate the object , That is, who should be instantiated , Will instantiated objects be added in the future , For example, what should we do if we want to add new functions ? The simple factory approach just provides us with such a solution :
public class OperationFactory
{
public static Operation createOperate(String operate){
Operation oper = null;
switch(opertate){
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new OperationDiv();
break;
}
return oper;
}
}
ad locum Operation Is a calculation class , But we go through Switch case Different subclasses are implemented when instantiating , It can be said that operation It is actually a statement here , our Operation[a-z] All are operation Subclasses of , It uses polymorphic The nature of .
eg: Polymorphic interpretation
Animal obj = new Horse();
So let's assume that Horse Inherited Animal This class , So here's Animal It's just a statement , The actual situation of this object at run time is determined by the following new The object of horse decision , Parameterized polymorphism means that the name of a type can represent multiple types .
Did you see? ? We design in this way , Just enter the symbol of the operation , Factories can instantiate different objects , The method that returns the parent class through polymorphism also achieves the desired result .
Next, consider the following for such an approach Two questions :
1. If we want to change the behavior of addition , What to do ?
answer : Just change OperationAdd The class can
2. What if we want to add an operation , For example, add the operation of square root
answer : Add the corresponding operator class , And in switch case Add the corresponding branch to the table entry of
summary :
When the user does not know which concrete class instance to create , Or we usually use factory methods when we don't want to specify the instance to be created in the user code .
** The essence of the factory method is :** Let subclasses decide which class to instantiate , Thus, the instantiation of a class is delayed to its subclasses .
I believe you have a certain understanding of this simple factory method , If you are interested, you can take a look at **《 Big talk design patterns 》** Abstract factory methods in , I believe you will understand the design subtlety and corresponding disadvantages of the factory method .
This blog is over here , Thanks for reading !
边栏推荐
- Software configuration item change and baseline change
- 高速缓冲存储器Cache地址映像与变换及相联目录表的设计
- Is it safe to open an account in an external market or domestic futures?
- The golden band is the fourth largest operator's mace, and farmers will get low-cost 5g services
- 颠覆认知!数据增强、正则化可导致不易察觉的灾难?
- In WinCC, how to use C script to set + reset + negate variables?
- 避免滥用class样式
- 机器学习基础备忘录
- 外盘开户期货,还是国内期货靠谱安全?
- 51 MCU timer 0 is used as time reference and delay function reference
猜你喜欢

I haven't published a thesis for 5 years, and I want to give up my doctoral degree? Ten thousand words self narration of the doctoral director of the University of science and technology of China: he

WordPress地址(URL)修改后打不开网站的解决方法

#yyds干货盘点# 解决剑指offer:矩形覆盖

PhD Debate | 自监督学习在推荐系统中的应用

Validate palindrome string

Wincc中,如何利用C脚本对变量进行置位+复位+取反操作?

使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'

Qiniu cloud backup website

How greatsql is a popular open source database in China

ECCV 2020 | STAR:基于Transformer的行人轨迹预测模型(二)
随机推荐
联调这夜,我把同事打了...
C listbox usage
Uniswapv2 peripheral contract learning (IX) -- examplecombinedswapadddremoveliquidity sol
MySQL transaction
MySQL数据库的存储引擎
临界区、事件、互斥量、 信号量--四种控制多线程同步与互斥的方法
使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的“心路(累)历程“
从刚入测试界到薪资翻倍:聊聊我的测试进阶历程,值得借鉴
The panorama of yuancosmos industrial investment, fast step into the new era of yuancosmos!
FCPX插件:动态物体运动模糊视觉特效Motion Blur FX by MA
验证回文串
《数字经济全景白皮书》银行财富管理篇 重磅发布
[leetcode question brushing record] sorting
[database] final review: SQL statement, definition and judgment of normal form, ER diagram to relational mode
WSL 安装的最佳实践
喜报 | 旺链科技签约汨罗市文旅体产业项目,打造“链”上数字乡村
insert into select/update
A solution to the problem that MySQL cannot update the query result set
Nerf neural radiation field eccv2020
How can minority majors solve the problem of "growth"?