实验5:建造者模式

实验5:建造者模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解建造者模式的动机,掌握该模式的结构;

2、能够利用建造者模式解决实际问题。

 

[实验任务一]:计算机组装

使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机的类型可以是笔记本,也可以是台式机。

实验要求:

1. 对应的类图如下

 

2. 源代码如下

 

 

 

package com.hua.sjms;
public abstract class Builder {
    Computer computer = new Computer();
    abstract public void buildZhuJi();
    abstract public void buildYinPan();
    abstract public void buildCpu();
    abstract public void buildNeiCun();
    public Computer getCompture()
    {
        return computer;
    }
}
package com.hua.sjms;
public class Creator {
    private Builder comptureKind;
    public void setCompture(Builder kind) {
        this.comptureKind = kind;
    }
    public Computer construct(){
        comptureKind.buildCpu();
        comptureKind.buildYinPan();
        comptureKind.buildZhuJi();
        comptureKind.buildNeiCun();
        return comptureKind.getCompture();
    }
}  
package com.hua.sjms;
public class Computer {
    private String cpu;
    private String neiCun;
    private String yinPan;
    private String zhuJi;
    public String getZhuJi() {
        return zhuJi;
    }
    public void setZhuJi(String zhuJi) {
        this.zhuJi = zhuJi;
    }
    public String getYinPan() {
        return yinPan;
    }
    public void setYinPan(String yinPan) {
        this.yinPan = yinPan;
    }
    public String getNeiCun() {
        return neiCun;
    }
    public void setNeiCun(String neiCun) {
        this.neiCun = neiCun;
    }
    public String getCpu() {
        return cpu;
    }
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }
}  
package com.hua.sjms;
public class LapBuilder extends Builder{
    @Override
    public void buildZhuJi() {
        computer.setZhuJi("武极");
    }
    @Override
    public void buildYinPan() {
        computer.setYinPan("512G");
    }
    @Override
    public void buildCpu() {
        computer.setCpu("因特尔");
    }
    @Override
    public void buildNeiCun() {
        computer.setNeiCun("16G");
    }
}  
package com.hua.sjms;
public class TaiShiBuilder extends Builder{
    @Override
    public void buildZhuJi() {
        computer.setZhuJi("联想");
    }
    @Override
    public void buildYinPan() {
        computer.setYinPan("1T");
    }
    @Override
    public void buildCpu() {
        computer.setCpu("英伟达");
    }
    @Override
    public void buildNeiCun() {
        computer.setNeiCun("16G");
    }
}  
package com.hua.sjms;
public class Client {
    public static void main(String[] args) {
        Builder lap;
        Creator factory=new Creator();
        for (int i = 0; i < 2; i++) {
            if(i==1) {
                System.out.println("笔记本配置如下");
                lap = new LapBuilder();
            }
            else {
                System.out.println("台式机配置如下");
                lap = new TaiShiBuilder();
            }
            factory.setCompture(lap);
            factory.construct();
            System.out.println("CPU:"+lap.getCompture().getCpu());
            System.out.println("内存:"+lap.getCompture().getNeiCun());
            System.out.println("硬盘:"+lap.getCompture().getYinPan());
            System.out.println("主机:"+lap.getCompture().getZhuJi());
        }
    }
}  

  

 

posted @ 2023-11-25 07:48  杳无音讯  阅读(27)  评论(0编辑  收藏  举报