一、结构分析
组建一台电脑,需要多个部件,包括硬盘、内存、显卡等等,如何选择获取自己所需要配置的配件,可以通过抽象工厂模式来对其进行实现。而如何将这些部件组装成一台完整的电脑并返回给用户,则可以通过建造者模式来对已选择好的电脑配件进行组装,下面将以硬盘和内存为例,使用抽象工厂模式和建造者模式组装一台电脑。
1.电脑配置结构图
2.电脑配置类图设计(抽象工厂模式)
3.电脑构建类图设计(建造者模式)
二、代码实现
1.抽象工厂模式实现电脑部件的配置
a.创建部件的抽象类(接口)
硬盘接口
package work8_2022_09_26;
/*
* 硬盘的抽象工厂
* */
public interface Hardpan {
void show();
public String getUNIT();
public String getCONFIGURE();
public String getBRAND();
}
内存接口
package work8_2022_09_26;
/*
* 内存的抽象工厂
* */
public interface Storage {
void show();
public String getUNIT();
public String getCONFIGURE();
public String getBRAND();
}
b.实现接口
包括硬盘、内存、品牌不同搭配的八种组合
硬盘 + 30G + HD品牌
package work8_2022_09_26;
public class HardpanHD_30G implements Hardpan{
private static String UNIT = "硬盘";
private static String CONFIGURE = "30G";
private static String BRAND = "HD";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
硬盘 + 500G + HD品牌
package work8_2022_09_26;
public class HardpanHD_500G implements Hardpan{
private static String UNIT = "硬盘";
private static String CONFIGURE = "500G";
private static String BRAND = "HD";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
硬盘 + 30G + 联想品牌
package work8_2022_09_26;
public class HardpanLe_30G implements Hardpan{
private static String UNIT = "硬盘";
private static String CONFIGURE = "30G";
private static String BRAND = "联想";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
硬盘 + 500G + 联想品牌
package work8_2022_09_26;
public class HardpanLe_500G implements Hardpan{
private static String UNIT = "硬盘";
private static String CONFIGURE = "500G";
private static String BRAND = "联想";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
内存 + 8G + HD品牌
package work8_2022_09_26;
public class StorageHD_8G implements Storage{
private static String UNIT = "内存卡";
private static String CONFIGURE = "8G";
private static String BRAND = "HD";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
内存 + 16G + HD品牌
package work8_2022_09_26;
public class StorageHD_16G implements Storage{
private static String UNIT = "内存卡";
private static String CONFIGURE = "16G";
private static String BRAND = "HD";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
内存 + 8G + 联想
package work8_2022_09_26;
/*
* 一个部件
* 联想的内存卡
* 其内存为8G
* */
public class StorageLe_8G implements Storage{
private static String UNIT = "内存卡";
private static String CONFIGURE = "8G";
private static String BRAND = "联想";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
内存 + 16G + 联想
package work8_2022_09_26;
public class StorageLe_16G implements Storage{
private static String UNIT = "内存卡";
private static String CONFIGURE = "16G";
private static String BRAND = "联想";
@Override
public void show() {
System.out.println("**************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(UNIT + "\t\t" + CONFIGURE + "\t\t" + BRAND);
System.out.println("**************************************");
}
public String getUNIT() {
return UNIT;
}
public String getCONFIGURE() {
return CONFIGURE;
}
public String getBRAND() {
return BRAND;
}
}
b.创建一个品牌的抽象工厂(接口)
工厂能生产四种部件
package work8_2022_09_26;
/*
* 品牌选择的抽象工厂
* */
public interface BrandFactory {
Hardpan createHardpan_500G();
Storage createStorage_8G();
Hardpan createHardpan_30G();
Storage createStorage_16G();
}
c.分别创建两个具体工厂
HD品牌的工厂
package work8_2022_09_26;
/*
* HD工厂的实现类
* */
public class HDFactory implements BrandFactory {
@Override
public Hardpan createHardpan_500G() {
// TODO 自动生成的方法存根
return new HardpanHD_500G();
}
@Override
public Storage createStorage_8G() {
// TODO 自动生成的方法存根
return new StorageHD_8G();
}
@Override
public Hardpan createHardpan_30G() {
// TODO 自动生成的方法存根
return new HardpanHD_30G();
}
@Override
public Storage createStorage_16G() {
// TODO 自动生成的方法存根
return new StorageHD_16G();
}
}
联想品牌的工厂
package work8_2022_09_26;
/*
* 联想的实例工厂
* */
public class LenovoFactory implements BrandFactory{
@Override
public Hardpan createHardpan_500G() {
// TODO 自动生成的方法存根
return new HardpanLe_500G();
}
@Override
public Storage createStorage_8G() {
// TODO 自动生成的方法存根
return new StorageLe_8G();
}
@Override
public Hardpan createHardpan_30G() {
// TODO 自动生成的方法存根
return new HardpanLe_30G();
}
@Override
public Storage createStorage_16G() {
// TODO 自动生成的方法存根
return new StorageLe_16G();
}
}
d.创建测试类
package work8_2022_09_26;
public class Test {
public static void main(String[] args) {
//指定品牌工厂
//创建品牌对象
BrandFactory factory1 = new HDFactory();
BrandFactory factory2 = new LenovoFactory();
//指定的品牌工厂开始生产需要的内存卡和硬盘
Storage storage = factory1.createStorage_8G();
Hardpan hardpan = factory2.createHardpan_500G();
storage.show();//打印配置
hardpan.show();//打印配置
}
}
c.测试结果如下
2.组装一台电脑
a.建立一个电脑类
package work8_2022_09_26;
/*
* 电脑类,及需要构建的产品
* */
public class Computer {
private Storage storage;
private Hardpan hardpan;
public Storage getStorage() {
return storage;
}
public void setStorage(Storage storage) {
this.storage = storage;
}
public Hardpan getHardpan() {
return hardpan;
}
public void setHardpan(Hardpan hardpan) {
this.hardpan = hardpan;
}
//电脑当前配置显示
public void show() {
System.out.println("********************************************");
System.out.println("部件\t\t配置\t\t品牌");
System.out.println(storage.getUNIT() + "\t\t" + storage.getCONFIGURE() + "\t\t" + storage.getBRAND());
System.out.println(hardpan.getUNIT() + "\t\t" + hardpan.getCONFIGURE() + "\t\t" + hardpan.getBRAND());
System.out.println("********************************************");
}
}
b.创建抽象建造者类
package work8_2022_09_26;
/*
* 建造者
* 即电脑组装人员
* */
public abstract class Buildder {
//饿汉模式
protected Computer computer = new Computer();
public abstract void buildStorage(Storage storage);
public abstract void buildHardpan(Hardpan hardpan);
//返回产品对象
public Computer getResult() {
return computer;
}
}
c.创建具体建造者类,实现抽象建造者的组装方法
package work8_2022_09_26;
/*
* 具体构造类
* 实现构建的方法
* */
public class ConcreteBuilder extends Buildder{
@Override
public void buildStorage(Storage storage) {
System.out.println("正在组装内存卡.......");
computer.setStorage(storage);
}
@Override
public void buildHardpan(Hardpan hardpan) {
computer.setHardpan(hardpan);
System.out.println("正在组装硬盘.......");
}
}
d.创建指挥者类,对获取的部件进行指挥组装
package work8_2022_09_26;
/*
* 指挥者类
* 可以通过指挥者控制产品的组装顺序
* */
public class Director {
private Buildder buildder;
public Director(Buildder buildder) {
super();
this.buildder = buildder;
}
public Buildder getBuildder() {
return buildder;
}
public void setBuildder(Buildder buildder) {
this.buildder = buildder;
}
//电脑组装和构建方法
public Computer Construct(Storage storage,Hardpan hardpan) {
System.out.println("开始组装.........");
//可以控制构建顺序
buildder.buildHardpan(hardpan);
buildder.buildStorage(storage);
System.out.println("组装成功!");
return buildder.getResult();
}
}
三、最终测试
1.Test测试类
package work8_2022_09_26;
public class Test {
public static void main(String[] args) {
//指定品牌工厂
//创建品牌对象
BrandFactory factory1 = new HDFactory();
BrandFactory factory2 = new LenovoFactory();
//指定的品牌工厂开始生产需要的内存卡和硬盘
Storage storage = factory1.createStorage_8G();
Hardpan hardpan = factory2.createHardpan_500G();
storage.show();//打印配置
hardpan.show();//打印配置
Buildder buildder = new ConcreteBuilder();
Director director = new Director(buildder);
Computer computer = director.Construct(storage, hardpan);
System.out.println("当前电脑配置如下:");
computer.show();
}
}