当前位置:网站首页>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 .
边栏推荐
猜你喜欢

ArrayList and sequence table

时间复杂度与空间复杂度

ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528

The jetpack compose dropdownmenu is displayed following the finger click position

The first China Digital Collection conference will be held soon

Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement

LVGL 8.2 Image

OceanBase 安装 yum 源配置错误及解决办法

List introduction

The intelligent DNA molecular nano robot model is coming
随机推荐
LVGL 8.2 Drop down in four directions
LVGL 8.2 Checkboxes as radio buttons
LeetCode Algorithm 86. Separate linked list
压缩状态DP位运算
From introduction to mastery of MySQL 50 lectures (32) -scylladb production environment cluster building
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
What is erdma as illustrated by Coptic cartoon?
List introduction
Time complexity and space complexity
SQL必需掌握的100个重要知识点:汇总数据
焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
LED driver library based on Hal Library
IDEA 又出新神器,一套代码适应多端!
数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】
记一次ViewPager + RecyclerView的内存泄漏
File sharing server
Iptables target tproxy
8行代码实现快速排序,简单易懂图解!
LVGL 8.2 re-coloring
【IC5000教程】-01-使用daqIDEA图形化debug调试C代码