当前位置:网站首页>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.
边栏推荐
- Lightroom import picture gray / Black rectangular multi display
- 【微服务】Nacos集群搭建以及加载文件配置
- 740. Delete and get points
- Sleep quality today 78 points
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- 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?
- Learning multi-level structural information for small organ segmentation
- QT qtablewidget table column top requirements ideas and codes
- R统计绘图-随机森林分类分析及物种丰度差异检验组合图
- C实现贪吃蛇小游戏
猜你喜欢

AWT common components, FileDialog file selection box

JSON web token -- comparison between JWT and traditional session login authentication

C语言中的函数(详解)

C實現貪吃蛇小遊戲

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?

Json Web token - jwt vs. Traditional session login Authentication

webrtc 快速搭建 视频通话 视频会议

JSON Web Token----JWT和傳統session登錄認證對比

分布式CAP理论
![70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment](/img/3f/36a67544deceb3d3789b500efde89c.jpg)
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
随机推荐
JSON Web Token----JWT和传统session登录认证对比
Appium基础 — APPium安装(二)
lightroom 导入图片灰色/黑色矩形 多显示器
Lightroom import picture gray / Black rectangular multi display
Design and implementation of redis 7.0 multi part AOF
Notes and notes
Arcpy uses the updatelayer function to change the symbol system of the layer
每周小结(*63):关于正能量
InputStream/OutputStream(文件的输入输出)
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
MySQL learning notes 3 - JDBC
微信小程序使用rich-text中图片宽度超出问题
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
Detectron: train your own data set -- convert your own data format to coco format
Sleep quality today 78 points
如何实现视频平台会员多账号登录
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Weekly summary (*63): about positive energy
Abap:ooalv realizes the function of adding, deleting, modifying and checking
buuctf-pwn write-ups (8)