当前位置:网站首页>抽象工厂及其改进示例
抽象工厂及其改进示例
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类图:
边栏推荐
- Uninstall Meizu app store
- .NET操作Redis List列表
- [Halcon vision] Fourier transform of image
- [Halcon vision] morphological expansion
- Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
- PLC overview
- [Halcon vision] morphological corrosion
- [Halcon vision] image filtering
- Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
- 事务的传播性propagation
猜你喜欢
[Halcon vision] image gray change
Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
Write to esp8266 burning brush firmware
粽子大战 —— 猜猜谁能赢
2022/07/25------字符串的排列
多目标优化系列1---NSGA2的非支配排序函数的讲解
SAP ABAP Netweaver 容器化的一些前沿性研究工作分享
QRcode二维码(C语言)遇到的问题
随机推荐
Controller返回JSON数据
[Halcon vision] threshold segmentation
Inheritance method of simplified constructor (II) - class inheritance in ES6
上传图片获取宽高
[Halcon vision] array
Using native JS to realize custom scroll bar (click to reach, drag to reach)
Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
2022pta平时训练题(1~10题字符串处理问题)
比较器(Comparable与Comparator接口)
[Halcon vision] software programming ideas
Unit test, what is unit test and why is it so difficult to write a single test
centos8(liunx)部署WTM(ASP.NET 5)使用pgsql
algorithm
[C language] LINQ overview
Structure of [Halcon vision] operator
Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
卸载魅族应用商店
抓包工具fiddler和wireshark对比
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
[Halcon vision] programming logic