当前位置:网站首页>抽象工厂及其改进示例
抽象工厂及其改进示例
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类图:
边栏推荐
- uniapp使用简单方法signalR(仅用于web调试,无法打包app)
- 12 复制对象时勿忘其每一个成分
- 【dectectron2】跟着官方demo一起做
- STM32 Alibaba cloud mqtt esp8266 at command
- Our Web3 entrepreneurship project is yellow
- [socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
- PLC overview
- SuperMap IClient for Leaflet 加载高斯克吕格投影三度分带CGCS2000大地坐标系WMTS服务
- .NET 开源框架在工业生产中的应用
- 【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)
猜你喜欢

Dry goods likeshop takeout order system is open source, 100% open source, no encryption

Unit test, what is unit test and why is it so difficult to write a single test
![[Halcon vision] array](/img/29/905d93795a24538fded18d2d377e52.png)
[Halcon vision] array

议程速递 | 7月27日分论坛议程一览
![[leetcode每日一题2021/2/14]765. 情侣牵手](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode每日一题2021/2/14]765. 情侣牵手

canvas上传图片base64-有裁剪功能-Jcrop.js

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

.net5wtm (asp.net core) PgSQL unpacking operation

Comparison of packet capturing tools fiddler and Wireshark

Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
随机推荐
Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
Redis Docker实例与数据结构
图片随手机水平移动-陀螺仪。360度设置条件
【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)
[Halcon vision] software programming ideas
About the declaration and definition of template functions [easy to understand]
cavans实现静态滚动弹幕
2022/07/25 ------ arrangement of strings
超图 影像 如何去除黑边(两种方法)
.NET 开源框架在工业生产中的应用
【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)
Redis special data type usage scenarios
[Halcon vision] Fourier transform of image
MD5 encryption
MD5加密
Redis implementation of distributed lock solution
链式方法调用的事务问题剖析
2022pta平时训练题(1~10题字符串处理问题)
[C language] LINQ overview
异常的概念与处理