当前位置:网站首页>Abstract factory and its improvement examples
Abstract factory and its improvement examples
2022-07-26 13:23:00 【Suzerk】
This article will use Reflection mechanism and configuration file improvement Abstract factory .
Abstract factory example
The structure is as follows :
AbstractComputer,AbstractDisplay be similar , They are all abstract products .
public class AbstractComputer {
protected String manufacturerName="";
protected String modelName="";
public String showDetails(){
return " The manufacturer is "+manufacturerName+" Models for "+modelName;
}
}
AppleComputer,DellComputer Class inherited AbstractComputer, Realize specific products . The relationship between abstract products and specific products for display types is similar , The code is as follows :
public class AppleComputer extends AbstractComputer{
protected String manufacturerName="APPLE";
protected String modelName="Q1";
public String showDetails(){
return " The manufacturer is "+manufacturerName+" Models for "+modelName;
}
}
DeviceFactory Is an abstract factory class , Contains multiple ways to create a product , You can create Computer And other products of different types .
public abstract class DeviceFactory {
// Create a computer
abstract public AbstractComputer CreateComputer();
// Create a monitor
abstract public AbstractDisplay CreateDisplay();
}
AppleFactory Inherited DeviceFactory, Implement multiple abstract methods in the abstract factory , Complete the creation of specific products .DellFactory be similar .
public class AppleFactory extends DeviceFactory {
public AbstractComputer CreateComputer(){
return new AppleComputer();
}
public AbstractDisplay
CreateDisplay(){
return new AppleDisplay();
}
}
Test client
public class Test {
public static void main(String[] args) {
AbstractComputer myPC;
AbstractDisplay myDisplay;
DeviceFactory factory=new DellFactory();
// DeviceFactory factory=new AppleFactory();
myPC=factory.CreateComputer();
myDisplay=factory.CreateDisplay();
System.out.println(" The computer :"+myPC.showDetails());
System.out.println(" Monitor :"+myDisplay.showDetails());
}
}
uml As shown in the figure :
Next, use reflection mechanism and configuration file to improve the abstract factory example .
Introduce configuration files into the structure , Delete all factories , Leave only DeviceFactory, Other classes remain unchanged .
The configuration file Device.properties
#DeviceComputer=AppleComputer
#DeviceDisplay=AppleDisplay
DeviceComputer=DellComputer
DeviceDisplay=DellDisplay
factory DeviceFactory
public class DeviceFactory {
public static Properties properties = new Properties();
public static File file = new File("src\\Device.properties");
public static AbstractComputer CreateComputer() throws Exception{
properties.load(new FileInputStream(file));
return (AbstractComputer)Class.forName(properties.getProperty("DeviceComputer")).newInstance();
};
public static AbstractDisplay CreateDisplay() throws Exception{
return (AbstractDisplay)Class.forName(properties.getProperty("DeviceDisplay")).newInstance();
};
}
Test client
public class Test {
public static void main(String[] args) throws Exception {
AbstractComputer myPC;
AbstractDisplay myDisplay;
myPC=DeviceFactory.CreateComputer();
myDisplay=DeviceFactory.CreateDisplay();
System.out.println(" The computer :"+myPC.showDetails());
System.out.println(" Monitor :"+myDisplay.showDetails());
}
}
uml Class diagram :
边栏推荐
- JSON格式执行计划(6)—mysql执行计划(五十二)
- B+ tree index uses (7) matching column prefix, matching value range (19)
- 牛客刷SQL---2
- We were tossed all night by a Kong performance bug
- 天津市应急局与驻津央企签署协议深化应急联动机制建设
- 终极套娃 2.0 | 云原生交付的封装
- Shutter background graying effect, how transparency, gray mask
- B+ tree index use (9) grouping, back to table, overlay index (21)
- 冒泡排序的时间复杂度分析
- 为什么要做“密评”?
猜你喜欢

天津市应急局与驻津央企签署协议深化应急联动机制建设

Hcip day 12 notes sorting (BGP Federation, routing rules)

3D modeling and rendering based on B é zier curve

Slam 02. overall framework

Hcip day 11 comparison (BGP configuration and release)

目标检测网络R-CNN 系列

Kubernetes APIServer 限流策略

Dimension disaster dimension disaster suspense

基于Bézier曲线的三维造型与渲染

Unity中序列化类为json格式
随机推荐
MySQL data directory (3) -- table data structure MyISAM (XXVI)
Create EOS account action
B+ tree (5) introduction to MyISAM -- MySQL from getting started to mastering (17)
牛客刷SQL---2
Learn about Pinia state getters actions plugins
Use float to realize left, middle and right layout, and the middle content is adaptive
B+ tree selection index (2) -- MySQL from entry to proficiency (23)
多线程使用不当导致的 OOM
Some practical operations of vector
Hcip day 11 comparison (BGP configuration and release)
华为机考 ~ 偏移量实现字符串加密
3D modeling and rendering based on B é zier curve
Slam 02. overall framework
Mysql数据目录(1)---数据库结构(二十四)
JSON format execution plan (6) - MySQL execution plan (52)
Activity.onStop() 延迟10秒?精彩绝伦的排查历程
Kubernetes flannel: host-gw mode
B+树索引使用(9)分组、回表、覆盖索引(二十一)
Unicode file parsing methods and existing problems
B+树(4)联合索引 --mysql从入门到精通(十六)