软件设计:实验5:建造者模式

实验5:建造者模式

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

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

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

 

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

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

实验要求:

1.画出对应的类图;

2.提交源代码;

3.注意编程规范。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.类图

 

 

 

2. 源代码

// 产品类:Computer

public class Computer {

    private CPU cpu;

    private Memory memory;

    private HardDisk hardDisk;

    private String type;

 

    private Computer(ComputerBuilder builder) {

        this.cpu = builder.cpu;

        this.memory = builder.memory;

        this.hardDisk = builder.hardDisk;

        this.type = builder.type;

    }

 

    public ComputerBuilder getBuilder() {

        return new ComputerBuilder(this);

    }

 

    // Getters and setters

    public CPU getCpu() {

        return cpu;

    }

 

    public void setCpu(CPU cpu) {

        this.cpu = cpu;

    }

 

    public Memory getMemory() {

        return memory;

    }

 

    public void setMemory(Memory memory) {

        this.memory = memory;

    }

 

    public HardDisk getHardDisk() {

        return hardDisk;

    }

 

    public void setHardDisk(HardDisk hardDisk) {

        this.hardDisk = hardDisk;

    }

 

    public String getType() {

        return type;

    }

 

    public void setType(String type) {

        this.type = type;

    }

 

    @Override

    public String toString() {

        return "Computer{" +

                "cpu=" + cpu +

                ", memory=" + memory +

                ", hardDisk=" + hardDisk +

                ", type='" + type + '\'' +

                '}';

    }

 

    // Builder inner class

    public static class ComputerBuilder {

        private CPU cpu;

        private Memory memory;

        private HardDisk hardDisk;

        private String type;

 

        public ComputerBuilder() {

        }

 

        public ComputerBuilder(Computer computer) {

            this.cpu = computer.cpu;

            this.memory = computer.memory;

            this.hardDisk = computer.hardDisk;

            this.type = computer.type;

        }

 

        public ComputerBuilder setCPU(CPU cpu) {

            this.cpu = cpu;

            return this;

        }

 

        public ComputerBuilder setMemory(Memory memory) {

            this.memory = memory;

            return this;

        }

 

        public ComputerBuilder setHardDisk(HardDisk hardDisk) {

            this.hardDisk = hardDisk;

            return this;

        }

 

        public ComputerBuilder setType(String type) {

            this.type = type;

            return this;

        }

 

        public Computer build() {

            return new Computer(this);

        }

    }

}

 

// Component classes

class CPU {

    private String brand;

 

    public CPU(String brand) {

        this.brand = brand;

    }

 

    @Override

    public String toString() {

        return "CPU{" +

                "brand='" + brand + '\'' +

                '}';

    }

}

 

class Memory {

    private int size;

 

    public Memory(int size) {

        this.size = size;

    }

 

    @Override

    public String toString() {

        return "Memory{" +

                "size=" + size +

                '}';

    }

}

 

class HardDisk {

    private int size;

 

    public HardDisk(int size) {

        this.size = size;

    }

 

    @Override

    public String toString() {

        return "HardDisk{" +

                "size=" + size +

                '}';

    }

}

posted @ 2024-11-27 17:01  痛苦代码源  阅读(7)  评论(0编辑  收藏  举报