软件设计 石家庄铁道大学信息学院
实验5:建造者模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解建造者模式的动机,掌握该模式的结构;
2、能够利用建造者模式解决实际问题。
[实验任务一]:计算机组装
使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机的类型可以是笔记本,也可以是台式机。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
Client:
package builder;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
System.out.println("请选择组装的电脑类型:1 笔记本 2 台式机");
Scanner sc = new Scanner(System.in);
String type = sc.nextLine();
if(type.equals("1")) {
Director director = new Director();
Builder builder = new Laptop();
director.setBuilder(builder);
Computer computer = director.construct();
System.out.println("组装进行:");
System.out.println(computer.getCpu());
System.out.println(computer.getNeichun());
System.out.println(computer.getYingjian());
System.out.println(computer.getZhuji());
}else if(type.equals("2")) {
Director director = new Director();
Builder builder = new Desktop();
director.setBuilder(builder);
Computer computer = director.construct();
System.out.println("组装进行:");
System.out.println(computer.getCpu());
System.out.println(computer.getNeichun());
System.out.println(computer.getYingjian());
System.out.println(computer.getZhuji());
}
}
}
Bulider:
package builder;
public abstract class Builder {
protected Computer computer = new Computer();
public abstract void buildCpu();
public abstract void buildNeichun();
public abstract void buildYingpan();
public abstract void buildZhuji();
public Computer getResult()
{
return computer;
}
}
Computer:
package builder;
public class Computer {
private String cpu;
private String neichun;
private String yingjian;
private String zhuji;
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public String getNeichun() {
return neichun;
}
public void setNeichun(String neichun) {
this.neichun = neichun;
}
public String getYingjian() {
return yingjian;
}
public void setYingjian(String yingjian) {
this.yingjian = yingjian;
}
public String getZhuji() {
return zhuji;
}
public void setZhuji(String zhuji) {
this.zhuji = zhuji;
}
}
Desktop:
package builder;
public class Desktop extends Builder{
@Override
public void buildCpu() {
computer.setCpu("台式CPU");
}
@Override
public void buildNeichun() {
computer.setNeichun("台式内存");
}
@Override
public void buildYingpan() {
computer.setYingjian("台式硬件");
}
@Override
public void buildZhuji() {
computer.setZhuji("台式主机");
}
}
Laptop:
package builder;
public class Laptop extends Builder{
@Override
public void buildCpu() {
computer.setCpu("笔记本CPU");
}
@Override
public void buildNeichun() {
computer.setNeichun("笔记本内存");
}
@Override
public void buildYingpan() {
computer.setYingjian("笔记本硬件");
}
@Override
public void buildZhuji() {
computer.setZhuji("笔记本主机");
}
}
Director:
package builder;
public class Director {
public Builder builder;
public void setBuilder(Builder builder)
{
this.builder = builder;
}
public Computer construct()
{
builder.buildCpu();
builder.buildZhuji();
builder.buildNeichun();
builder.buildYingpan();
return builder.getResult();
}
}