当前位置:网站首页>8. Factory method
8. Factory method
2022-07-04 06:20:00 【Muzi's day and night】
package factoryMethodModel;
/**
* Factory method model
*
* @author LiMing E-mail:[email protected]
* @date 2017 year 6 month 14 Japan In the morning 8:19:28
*
*
* The simple factory model has been mentioned before : The disadvantage is that it violates the principle of openness and closeness ( Open to expansion But the modification is not closed Each time you add a new feature, you need to modify the factory class )
* The factory method mode is an upgrade of the simple factory mode It has the advantage of simple factory mode And the principle of openness and closeness Because he is to create each kind of object as a factory
* So if you need to add a new category Just add a new subclass factory method There is no need to modify the parent class factory method ( Modify closure ) See below for specific implementation
*
* The only drawback of the factory approach is to put the logic judgment on the client side Later, it can be reflected Yes, it is reflection to simplify the operation of the client
*
*/
public class FactoryMethodModel {
public static void main(String[] args) {
// Use addition
Oprator add = AddFactory.createOprator();
add.method();
Oprator reduce = ReduceFactory.createOprator();
reduce.method();
/*
* Right now I want to add a multiplication method Just add a multiplication execution method class Multiplication and Multiplication constructs factories
* There is no need to modify any existing classes And then when we're done It's very convenient to use
*/
Oprator mltiplication = MultiplicationFactory.createOprator();
mltiplication.method();
}
}
/**
* Abstract factory class
*/
class Factory {
public static Oprator createOprator() {
return new Oprator();
}
}
/**
* Additive factory
*/
class AddFactory extends Factory {
public static Oprator createOprator() {
return new Add();
}
}
/**
* Subtraction factory
*/
class ReduceFactory extends Factory {
public static Oprator createOprator() {
return new Reduce();
}
}
/**
* Operation parent class
*/
class Oprator {
public void method() {
System.out.println(" Parent class operation method ");
}
}
/*
* Add class
*/
class Add extends Oprator {
public void method() {
System.out.println(" Perform addition ");
}
}
/*
* Subtraction class
*/
class Reduce extends Oprator {
public void method() {
System.out.println(" Perform subtraction ");
}
}
/*
* Add multiplication
*/
// Operation class
class Multiplication extends Oprator {
public void method() {
System.out.println(" Perform multiplication ");
}
}
// Factory
class MultiplicationFactory extends Factory {
public static Oprator createOprator() {
return new Multiplication();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
边栏推荐
- Compound nonlinear feedback control (2)
- JSON web token -- comparison between JWT and traditional session login authentication
- 27-31. Dependency transitivity, principle
- QT 获取随机颜色值设置label背景色 代码
- 测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
- Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
- Considerations for testing a website
- Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
- Weekly summary (*63): about positive energy
- Invalid revision: 3.18.1-g262b901-dirty
猜你喜欢
Sleep quality today 78 points
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
Configure cross compilation tool chain and environment variables
Win10 clear quick access - leave no trace
Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
740. Delete and get points
复合非线性反馈控制(二)
Design and implementation of redis 7.0 multi part AOF
Uninstall Google drive hard drive - you must exit the program to uninstall
随机推荐
云原生——上云必读之SSH篇(常用于远程登录云服务器)
[March 3, 2019] MAC starts redis
Abap:ooalv realizes the function of adding, deleting, modifying and checking
Design and implementation of redis 7.0 multi part AOF
Learning multi-level structural information for small organ segmentation
QT releases multilingual International Translation
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
Dimension and format of data
C實現貪吃蛇小遊戲
Vant --- detailed explanation and use of list component in vant
Compound nonlinear feedback control (2)
FRP intranet penetration, reverse proxy
js arguments参数使用和详解
How to use multithreading to export excel under massive data? Source code attached!
JS flattened array of number shape structure
Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
Fast power (template)
24 magicaccessorimpl can access the debugging of all methods
Notes and notes
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage