当前位置:网站首页>创建型-配置工厂
创建型-配置工厂
2022-06-30 10:39:00 【vbirdbest】
实际工作中可能会遇到“配置工厂”这种做法,这中做法并不属于设计模式,大概实现思路将要创建的对象配置到properties文件中,然后读取这个配置文件,使用反射创建对象存储在一个静态全局变量中,然后当客户端获取的时候从全局对象中取即可。
一:entity
public abstract class Coffee {
public abstract String getName();
}
public class AmericanCoffee extends Coffee {
@Override
public String getName() {
return "美式咖啡(美式风味)";
}
}
public class LatteCoffee extends Coffee {
@Override
public String getName() {
return "拿铁咖啡(意大利风味)";
}
}
二:bean.properties
American=com.example.design.AmericanCoffee
Latte=com.example.design.LatteCoffee
三:ConfigFactory
public class CoffeeConfigFactory {
private static Map<String, Coffee> coffeeMap = new HashMap<>();
static {
InputStream inputStream = CoffeeConfigFactory.class.getClassLoader().getResourceAsStream("bean.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
for (Object key : properties.keySet()) {
String classFullName = (String) properties.get(key);
Coffee coffee = (Coffee)Class.forName(classFullName).newInstance();
coffeeMap.put((String) key, coffee);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Coffee createCoffee(String name) {
return coffeeMap.get(name);
}
}
四:Main
public static void main(String[] args) {
Coffee coffee = CoffeeConfigFactory.createCoffee("Latte");
System.out.println(coffee.getName());
}
五:与工厂方法设计模式的比较
- 当新增一个对象时工厂方法模式需要新增一个对应的工厂类文件,而配置工厂这种做法只需要在配置文件新增配置即可,这种做法更简单。
- 配置工厂是在项目启动的时候会创建出所有的对象永驻到内存中不会被回收,用内存来换取方便。
边栏推荐
- Pytorch Notebook. Nn. Batchnorm1d
- Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
- Didi open source agile test case management platform!
- My in-depth remote office experience | community essay solicitation
- 中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
- SQL必需掌握的100个重要知识点:汇总数据
- Auto SEG loss: automatic loss function design
- 再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
- [rust daily] the first rust monthly magazine on January 22, 2021 invites everyone to participate
- The life, working principle and application of electrochemical oxygen sensor
猜你喜欢

从开源项目探讨“FPGA挖矿”的本质

【STL源码剖析】迭代器

Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change

小程序中读取腾讯文档的表格数据

CSDN blog operation team 2022 H1 summary

Jetpack Compose DropdownMenu跟随手指点击位置显示

煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗

Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)

中移OneOS开发板学习入门

Anhui "requirements for design depth of Hefei fabricated building construction drawing review" was printed and distributed; Hebei Hengshui city adjusts the pre-sale license standard for prefabricated
随机推荐
The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
The life, working principle and application of electrochemical oxygen sensor
[proteus simulation] Arduino uno led simulated traffic light
[untitled]
ArrayList and sequence table
深潜Kotlin协程(十七):演员
Matplotlib notes: contour & Contour
Pandora IOT development board learning (HAL Library) - Experiment 1 running lantern (RGB) experiment (learning notes)
Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...
Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
LeetCode Algorithm 86. 分隔鏈錶
LVGL 8.2 Simple Colorwheel
中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
内存逃逸分析
pytorch 筆記 torch.nn.BatchNorm1d
Algorithme leetcode 86. Liste des liens séparés
05_ Node JS file management module FS
Lvgl 8.2 picture scaling and rotation
LED driver library based on Hal Library
LVGL 8.2 Checkboxes as radio buttons