当前位置:网站首页>Create - configure factory
Create - configure factory
2022-06-30 11:13:00 【vbirdbest】
You may encounter “ Configuration factory ” This kind of practice , This is not a design pattern , The general implementation idea is to configure the object to be created to properties In file , Then read the configuration file , Use reflection to create objects stored in a static global variable , Then, when the client gets it, it can get it from the global object .
One :entity
public abstract class Coffee {
public abstract String getName();
}
public class AmericanCoffee extends Coffee {
@Override
public String getName() {
return " Cafe Americano ( American flavor )";
}
}
public class LatteCoffee extends Coffee {
@Override
public String getName() {
return " Cafe Latte ( Italian flavor )";
}
}
Two :bean.properties
American=com.example.design.AmericanCoffee
Latte=com.example.design.LatteCoffee
3、 ... and :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);
}
}
Four :Main
public static void main(String[] args) {
Coffee coffee = CoffeeConfigFactory.createCoffee("Latte");
System.out.println(coffee.getName());
}
5、 ... and : Comparison with factory method design pattern
- When adding an object, the factory method pattern needs to add a corresponding factory class file , To configure a factory, you only need to add a new configuration in the configuration file , This is easier .
- The configuration factory will create all objects that will be permanently stored in memory and will not be recycled when the project is started , Exchange memory for convenience .
边栏推荐
- sublist3r报错解决
- ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528
- LVGL 8.2 Image
- The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
- LVGL 8.2 re-coloring
- Ant financial's written test question: what can be quantified in the requirements document? [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
- What is erdma as illustrated by Coptic cartoon?
- 时间复杂度与空间复杂度
- The reasoning delay on iphone12 is only 1.6 MS! Snap et al. Analyzed the transformer structure latency in detail, and used NAS to find out the efficient network structure of mobile devices
- Dickinson's soul chooses its companion
猜你喜欢

深潜Kotlin协程(十六):Channel

MySQL导出sql脚本文件

promise async和await的方法与使用

The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
![[STL source code analysis] container (to be supplemented)](/img/69/0c6e0e8ecb3ebc8c9b8503f5a8b4e5.jpg)
[STL source code analysis] container (to be supplemented)

Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)

Deep dive kotlin synergy (18): hot and cold data flow

8行代码实现快速排序,简单易懂图解!

Time complexity and space complexity

List introduction
随机推荐
时间复杂度与空间复杂度
SQL必需掌握的100个重要知识点:汇总数据
ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528
Wireguard simple configuration
SQL必需掌握的100个重要知识点:使用表别名
LVGL 8.2 Checkboxes as radio buttons
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
WireGuard简单配置
LVGL 8.2 Checkboxes as radio buttons
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
Understanding society at the age of 14 - reading notes on "happiness at work"
从开源项目探讨“FPGA挖矿”的本质
go语言defer
LVGL 8.2 Simple Colorwheel
导致系统性能失败的10个原因
What is erdma as illustrated by Coptic cartoon?
datax json说明
Memory escape analysis
LED driver library based on Hal Library