当前位置:网站首页>创建型-配置工厂
创建型-配置工厂
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());
}
五:与工厂方法设计模式的比较
- 当新增一个对象时工厂方法模式需要新增一个对应的工厂类文件,而配置工厂这种做法只需要在配置文件新增配置即可,这种做法更简单。
- 配置工厂是在项目启动的时候会创建出所有的对象永驻到内存中不会被回收,用内存来换取方便。
边栏推荐
- 【STL源码剖析】迭代器
- 基于HAL库的LED驱动库
- The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
- 【leetcode 16】三数之和
- datax - 艰难debug路
- My in-depth remote office experience | community essay solicitation
- When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
- 运动App如何实现端侧后台保活,让运动记录更完整?
- 文件共享服务器
- Agile Development: super easy to use bucket estimation system
猜你喜欢

WireGuard简单配置

Matplotlib notes: contour & Contour

Mysql database foundation: views and variables

The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)

【STL源码剖析】容器(待补充)

同事的接口文档我每次看着就头大,毛病多多。。。

Pytorch notes torch nn. BatchNorm1d

pytorch 筆記 torch.nn.BatchNorm1d

微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条...

数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】
随机推荐
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
[STL source code analysis] container (to be supplemented)
Auto SEG loss: automatic loss function design
国产自研系统的用户突破4亿,打破美国企业的垄断,谷歌后悔不迭
matplotlib 笔记: contourf & contour
ArrayList与顺序表
SQL必需掌握的100个重要知识点:插入数据
【STL源码剖析】容器(待补充)
LVGL 8.2 re-coloring
SQL必需掌握的100个重要知识点:使用表别名
SQL必需掌握的100个重要知识点:更新和删除数据
Mysql database foundation: views and variables
语音识别-基础(一):简介【语音转文本】
Qt之实现动效导航栏
Agile Development: super easy to use bucket estimation system
LVGL 8.2 Simple Drop down list
From introduction to mastery of MySQL 50 lectures (32) -scylladb production environment cluster building
无心剑中译狄金森《灵魂择其伴侣》
软件测试工程师面试基础题(应届生和测试小菜必备)最基础的面试题