azure011328

导航

 

软件设计                  石家庄铁道大学信息学院

 

实验5:建造者模式

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

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

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

 
   

 


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

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

实验要求:

 

public class Computer {

    private CPU cpu;

    private Memory memory;

    private HardDrive hardDrive;

    private Case caseType;

 

    public void setCPU(CPU cpu) {

        this.cpu = cpu;

    }

 

    public void setMemory(Memory memory) {

        this.memory = memory;

    }

 

    public void setHardDrive(HardDrive hardDrive) {

        this.hardDrive = hardDrive;

    }

 

    public void setCaseType(CaseType caseType) {

        this.caseType = caseType;

    }

 

    public void showSpecs() {

        System.out.println("CPU: " + cpu);

        System.out.println("Memory: " + memory);

        System.out.println("Hard Drive: " + hardDrive);

        System.out.println("Case Type: " + caseType);

    }

}

public interface ComputerBuilder {

    void buildCPU();

    void buildMemory();

    void buildHardDrive();

    void buildCaseType();

    Computer getComputer();

}

public class DesktopBuilder implements ComputerBuilder {

    private Computer computer = new Computer();

 

    @Override

    public void buildCPU() {

        computer.setCPU(new CPU("Desktop CPU"));

    }

 

    @Override

    public void buildMemory() {

        computer.setMemory(new Memory("Desktop Memory"));

    }

 

    @Override

    public void buildHardDrive() {

        computer.setHardDrive(new HardDrive("Desktop Hard Drive"));

    }

 

    @Override

    public void buildCaseType() {

        computer.setCaseType(CaseType.DESKTOP);

    }

 

    @Override

    public Computer getComputer() {

        return computer;

    }

}

 

public class LaptopBuilder implements ComputerBuilder {

    private Computer computer = new Computer();

 

    @Override

    public void buildCPU() {

        computer.setCPU(new CPU("Laptop CPU"));

    }

 

    @Override

    public void buildMemory() {

        computer.setMemory(new Memory("Laptop Memory"));

    }

 

    @Override

    public void buildHardDrive() {

        computer.setHardDrive(new HardDrive("Laptop Hard Drive"));

    }

 

    @Override

    public void buildCaseType() {

        computer.setCaseType(CaseType.LAPTOP);

    }

 

    @Override

    public Computer getComputer() {

        return computer;

    }

}

public class ComputerDirector {

    private ComputerBuilder builder;

 

    public ComputerDirector(ComputerBuilder builder) {

        this.builder = builder;

    }

 

    public Computer construct() {

        builder.buildCPU();

        builder.buildMemory();

        builder.buildHardDrive();

        builder.buildCaseType();

        return builder.getComputer();

    }

}

public class CPU {

    private String type;

 

    public CPU(String type) {

        this.type = type;

    }

 

    @Override

    public String toString() {

        return type;

    }

}

 

public class Memory {

    private String type;

 

    public Memory(String type) {

        this.type = type;

    }

 

    @Override

    public String toString() {

        return type;

    }

}

 

public class HardDrive {

    private String type;

 

    public HardDrive(String type) {

        this.type = type;

    }

 

    @Override

    public String toString() {

        return type;

    }

}

 

public enum CaseType {

    DESKTOP, LAPTOP

}

public class Client {

    public static void main(String[] args) {

        ComputerDirector director = new ComputerDirector(new DesktopBuilder());

        Computer desktop = director.construct();

        desktop.showSpecs();

 

        director = new ComputerDirector(new LaptopBuilder());

        Computer laptop = director.construct();

        laptop.showSpecs();

    }

}

posted on 2024-11-13 09:19  淮竹i  阅读(1)  评论(0编辑  收藏  举报