当前位置:网站首页>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.
边栏推荐
- Practical gadget instructions
- MySQL installation and configuration
- Qt发布多语言国际化翻译
- Distributed cap theory
- C language - Blue Bridge Cup - Snake filling
- How to avoid JVM memory leakage?
- SQL join, left join, right join usage
- win10清除快速访问-不留下痕迹
- 复合非线性反馈控制(二)
- Learning multi-level structural information for small organ segmentation
猜你喜欢

Impact relay jc-7/11/dc110v

ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station

云原生——上云必读之SSH篇(常用于远程登录云服务器)

How to get the parent node of all nodes in El tree

体验碎周报第 102 期(2022.7.4)

雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)

Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
![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](/img/a5/be967170a18cb2de097f01a6d4e039.jpg)
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

Learning multi-level structural information for small organ segmentation

AWT常用组件、FileDialog文件选择框
随机推荐
4G wireless all network solar hydrological equipment power monitoring system bms110
746. Climb stairs with minimum cost
Learning multi-level structural information for small organ segmentation
MySQL learning notes 3 - JDBC
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Gridview出现滚动条,组件冲突,如何解决
MySQL的information_schema数据库
Recommended system 1 --- framework
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
win10清除快速访问-不留下痕迹
Overview of convolutional neural network structure optimization
740. Delete and get points
Compound nonlinear feedback control (2)
【无标题】
Sort list tool class, which can sort strings
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
C實現貪吃蛇小遊戲
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Practical gadget instructions
ES6 modularization