当前位置:网站首页>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.
边栏推荐
- Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
- ES6 modularization
- Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
- 【微服务】Nacos集群搭建以及加载文件配置
- Sleep quality today 78 points
- 27-31. Dependency transitivity, principle
- How to avoid JVM memory leakage?
- 树形dp
- QT 获取随机颜色值设置label背景色 代码
- Learning multi-level structural information for small organ segmentation
猜你喜欢
Learning multi-level structural information for small organ segmentation
C language - Blue Bridge Cup - Snake filling
C language exercises (recursion)
Leetcode question brushing record | 206_ Reverse linked list
复合非线性反馈控制(二)
Distributed cap theory
24 magicaccessorimpl can access the debugging of all methods
我的NVIDIA开发者之旅——优化显卡性能
Design and implementation of redis 7.0 multi part AOF
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
随机推荐
Learning multi-level structural information for small organ segmentation
2022.7.3-----leetcode.556
How to realize multi account login of video platform members
Weekly summary (*63): about positive energy
ABAP:OOALV实现增删改查功能
QT QTableWidget 表格列置顶需求的思路和代码
C实现贪吃蛇小游戏
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
buuctf-pwn write-ups (8)
SQL join, left join, right join usage
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
AWT introduction
Practical gadget instructions
Gridview出现滚动条,组件冲突,如何解决
Nexus 6p从8.0降级6.0+root
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
How to expand all collapse panels
win10清除快速访问-不留下痕迹
JSON Web Token----JWT和傳統session登錄認證對比
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx