标签:String Computer void computer 实验 Override 设计模式 public
8u软件设计 石家庄铁道大学信息学院
实验5:建造者模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解建造者模式的动机,掌握该模式的结构;
2、能够利用建造者模式解决实际问题。
[实验任务一]:计算机组装
使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机的类型可以是笔记本,也可以是台式机。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
package qb;
public class Computer { private String cpu; private String memory; private String hardDisk; private String motherboard; private String type;
public void setCpu(String cpu) { this.cpu = cpu; }
public void setMemory(String memory) { this.memory = memory; }
public void setHardDisk(String hardDisk) { this.hardDisk = hardDisk; } public void setMotherboard(String motherboard) { this.motherboard = motherboard; }
public void setType(String type) { this.type = type; }
@Override public String toString() { return "Computer{" + "cpu='" + cpu + '\'' + ", memory='" + memory + '\'' + ", hardDisk='" + hardDisk + '\'' + ", motherboard='" + motherboard + '\'' + ", type='" + type + '\'' + '}'; } }
|
package qb;
public class ComputerAssemblyFactory { public static Computer assembleComputer(ComputerBuilder computerBuilder) { computerBuilder.buildCPU(); computerBuilder.buildMemory(); computerBuilder.buildHardDisk(); computerBuilder.buildMotherboard(); return computerBuilder.getComputer(); } }
|
package qb;
public interface ComputerBuilder { void buildCPU(); void buildMemory(); void buildHardDisk(); void buildMotherboard(); Computer getComputer(); }
|
package qb;
public class DesktopBuilder implements ComputerBuilder { private Computer computer;
public DesktopBuilder() { computer = new Computer(); computer.setType("Desktop"); }
@Override public void buildCPU() { computer.setCpu("Desktop CPU"); }
@Override public void buildMemory() { computer.setMemory("Desktop Memory"); }
@Override public void buildHardDisk() { computer.setHardDisk("Desktop Hard Disk"); }
@Override public void buildMotherboard() { computer.setMotherboard("Desktop Motherboard"); }
@Override public Computer getComputer() { return computer; } }
|
package qb;
public class LaptopBuilder implements ComputerBuilder { private Computer computer;
public LaptopBuilder() { computer = new Computer(); computer.setType("Laptop"); }
@Override public void buildCPU() { computer.setCpu("Laptop CPU"); }
@Override public void buildMemory() { computer.setMemory("Laptop Memory"); }
@Override public void buildHardDisk() { computer.setHardDisk("Laptop Hard Disk"); }
@Override public void buildMotherboard() { computer.setMotherboard("Laptop Motherboard"); }
@Override public Computer getComputer() { return computer; } }
|
package qb;
public class Main { public static void main(String[] args) { ComputerBuilder laptopBuilder = new LaptopBuilder(); Computer laptop = ComputerAssemblyFactory.assembleComputer(laptopBuilder); System.out.println(laptop);
ComputerBuilder desktopBuilder = new DesktopBuilder(); Computer desktop = ComputerAssemblyFactory.assembleComputer(desktopBuilder); System.out.println(desktop); } }
|
运行截图
3.注意编程规范。
标签:String,
Computer,
void,
computer,
实验,
Override,
设计模式,
public
From: https://www.cnblogs.com/azwz/p/17797233.html