当前位置:网站首页>抽象工厂及其改进示例
抽象工厂及其改进示例
2022-07-26 10:35:00 【Suzerk】
本文将用反射机制和配置文件改进抽象工厂。
抽象工厂示例
结构如下所示:
AbstractComputer,AbstractDisplay相似,他们都为抽象产品类。
public class AbstractComputer {
protected String manufacturerName="";
protected String modelName="";
public String showDetails(){
return "生产商为 "+manufacturerName+"型号为 "+modelName;
}
}
AppleComputer,DellComputer类继承了AbstractComputer,实现具体的产品。对于显示器类型的抽象产品与具体产品的关系相似,代码如下:
public class AppleComputer extends AbstractComputer{
protected String manufacturerName="APPLE";
protected String modelName="Q1";
public String showDetails(){
return "生产商为 "+manufacturerName+"型号为 "+modelName;
}
}
DeviceFactory为抽象工厂类,包含多个创建产品的方法,可以创建Computer等不同类型族的产品。
public abstract class DeviceFactory {
// 创造一台电脑
abstract public AbstractComputer CreateComputer();
// 创造一台显示器
abstract public AbstractDisplay CreateDisplay();
}
AppleFactory继承了DeviceFactory,实现抽象工厂中的多个抽象方法,完成具体产品的创建。DellFactory相似。
public class AppleFactory extends DeviceFactory {
public AbstractComputer CreateComputer(){
return new AppleComputer();
}
public AbstractDisplay
CreateDisplay(){
return new AppleDisplay();
}
}
Test客户端
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("电脑:"+myPC.showDetails());
System.out.println("显示器:"+myDisplay.showDetails());
}
}
uml如图所示:
接下来使用反射机制加配置文件改进该抽象工厂示例。
结构中引入配置文件,删去所有的工厂,只留下DeviceFactory,其他类保持不变。
配置文件Device.properties
#DeviceComputer=AppleComputer
#DeviceDisplay=AppleDisplay
DeviceComputer=DellComputer
DeviceDisplay=DellDisplay
工厂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客户端
public class Test {
public static void main(String[] args) throws Exception {
AbstractComputer myPC;
AbstractDisplay myDisplay;
myPC=DeviceFactory.CreateComputer();
myDisplay=DeviceFactory.CreateDisplay();
System.out.println("电脑:"+myPC.showDetails());
System.out.println("显示器:"+myDisplay.showDetails());
}
}
uml类图:
边栏推荐
- Interview questions and answers for the second company (2)
- 议程速递 | 7月27日分论坛议程一览
- Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
- Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
- [leetcode每日一题2021/4/29]403. 青蛙过河
- L2-005 set similarity (intersection of vector and set)
- 10 令 operator= 返回一个 reference to *this
- Using native JS to realize custom scroll bar (click to reach, drag to reach)
- 第8期:云原生—— 大学生职场小白该如何学
- 常见的类(了解)
猜你喜欢
![[Halcon vision] image filtering](/img/7a/b95f8977f02fab644ef9fb205424e7.png)
[Halcon vision] image filtering

干货likeshop外卖点餐系统开源啦100%开源无加密

2022/07/25------字符串的排列

The problem of large fluctuation of hx711 data

STM32 Alibaba cloud mqtt esp8266 at command

404页面和路由钩子

Unit test, what is unit test and why is it so difficult to write a single test

【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening

第7期:内卷和躺平,你怎么选
软件打不开了
随机推荐
Function template parameters (where are the function parameters)
Simple use of json-c Library -- converting JSON files to struct
移动端双指缩放事件(原生),e.originalEvent.touches
SAP ABAP Netweaver 容器化的一些前沿性研究工作分享
Application of.Net open source framework in industrial production
Redis Docker实例与数据结构
移动端H5开发常用技巧总结
Closure of go (cumulative sum)
分布式锁解决方案之Redis实现
Database functions
[Halcon vision] image gray change
Oracle创建索引
json_object_put: Assertion `jso->_ref_count > 0‘ failed.Aborted (core dumped)
vscode上使用anaconda(已经配置好环境)
软件打不开了
Tradingview tutorial
[Halcon vision] image filtering
A semicolon is missing
.NET操作Redis String字符串
Introduction to Phoenix (Level 1: Phoenix installation, level 2: Phoenix basic grammar)