当前位置:网站首页>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 .
边栏推荐
- 语音信号处理-基础(五):傅立叶变换
- 考研这些“不靠谱”的经验有多害人?
- 高通发布物联网案例集 “魔镜”、数字农业已经成为现实
- promise async和await的方法与使用
- Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...
- Deep dive kotlin Xie Cheng (17): Actor
- [untitled]
- When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
- SQL必需掌握的100个重要知识点:使用视图
- The life, working principle and application of electrochemical oxygen sensor
猜你喜欢

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

The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented

Methods and usage of promise async and await

深潜Kotlin协程(十六):Channel

微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供

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

在IPhone12的推理延迟仅为1.6 ms!Snap等详析Transformer结构延迟,并用NAS搜出移动设备的高效网络结构...

Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...

IDEA 又出新神器,一套代码适应多端!

OLAP数据库引擎如何选型?
随机推荐
100 important knowledge points that SQL must master: join table
datax json说明
The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
sublist3r报错解决
Mathematics (fast power)
时间复杂度与空间复杂度
Go语言学习之Switch语句的使用
高通发布物联网案例集 “魔镜”、数字农业已经成为现实
[机缘参悟-34]:光锥之内皆命运
[leetcode 16] sum of three numbers
LVGL8.2 Simple Checkboxes
LeetCode Algorithm 86. Separate linked list
SQL必需掌握的100个重要知识点:联结表
Rejuvenated Dell and apple hit each other, and the two old PC enterprises declined rapidly
go语言defer
Cp2112 teaching example of using USB to IIC communication
LVGL 8.2 Image styling and offset
The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
【STL源码剖析】容器(待补充)