建造者模式例题

        假定现在有这样一个需求:电脑可以由主板、硬盘、CPU、内存、显卡、显示器和键盘等元素构成,华硕公司可以生产里面的各种元素,Lenovo公司也可以生产里面的各种元素。假定你现在就想要一台电脑,这个电脑可以全部是来自华硕的品牌机,也可以是来自Lenovo的品牌机,也可以一部分元素来自华硕、另一部分来自Lenovo,请根据上述要求对代码进行设计,给出设计思路和源码。

        以下这段代码展示了一个简单的电脑组装系统。首先定义了不同的电脑元素接口,如主板、硬盘、CPU等。然后定义了电脑类 Computer,包含各种电脑组件,并通过 set 方法设置各个组件,最后通过 showInfo 方法展示电脑信息。

        接着有一个电脑建造者类 ComputerBuilder,其中包含了建造不同品牌电脑的方法,如 buildHSComputer(华硕电脑)、buildLenovoComputer(联想电脑)、buildCustomComputer(混合电脑),最后通过 getComputer 方法获取组装好的电脑。

        最后是 Director 类,包含了导演的角色,负责调用建造者类中的方法来组装不同品牌的电脑,并展示电脑的信息。

电脑元素接口:

//电脑元素接口
interface IMotherboard {

    void makeIMotherboard();

}

interface IHardDisk {

    void makeIHardDisk();

}

interface ICPU {

    void makeICPU();

}

interface IMemory {

    void makeIMemory();

}

interface IGraphicsCard {

    void makeIGraphicsCard();

}

interface IDisplay {

    void makeIDisplay();

}

interface IKeyboard {

    void makeIKeyboard();

}

电脑类

//电脑
public class Computer {

  
private IMotherboard motherboard;

    private IHardDisk hardDisk;

    private ICPU cpu;

    private IMemory memory;

    private IGraphicsCard graphicsCard;

    private IDisplay display;

    private IKeyboard keyboard;

    public void setMotherboard(IMotherboard motherboard) {

        this.motherboard = motherboard;

    }

    public void setHardDisk(IHardDisk hardDisk) {

        this.hardDisk = hardDisk;

    }

    public void setCpu(ICPU cpu) {

        this.cpu = cpu;

    }

    public void setMemory(IMemory memory) {

        this.memory = memory;

    }

    public void setGraphicsCard(IGraphicsCard graphicsCard) {

        this.graphicsCard = graphicsCard;

    }

    public void setDisplay(IDisplay display) {

        this.display = display;

    }

    public void setKeyboard(IKeyboard keyboard) {

        this.keyboard = keyboard;

    }

    public void showInfo() {

        if (motherboard != null) {

            motherboard.makeIMotherboard();

        }

        if (hardDisk != null) {

            hardDisk.makeIHardDisk();

        }

        if (cpu != null) {

            cpu.makeICPU();

        }

        if (memory != null) {

            memory.makeIMemory();

        }

        if (graphicsCard != null) {

            graphicsCard.makeIGraphicsCard();

        }

        if (display != null) {

            display.makeIDisplay();

        }

        if (keyboard != null) {

            keyboard.makeIKeyboard();

        }

    }

}

}

电脑建造者类

//电脑建造者
class ComputerBuilder {

    private Computer computer;

    public ComputerBuilder() {

        computer = new Computer();

    }

    public void buildHSComputer() {//华硕电脑

        computer.setMotherboard(new HSMotherboard());

        computer.setHardDisk(new HSHardDisk());

        computer.setCpu(new HSCPU());

        computer.setMemory(new HSMemory());

        computer.setGraphicsCard(new HSGraphicsCard());

        computer.setDisplay(new HSDisplay());

        computer.setKeyboard(new HSKeyboard());

    }

    public void buildLenovoComputer() {//联想电脑

        computer.setMotherboard(new LenovoMotherboard());

        computer.setHardDisk(new LenovoHardDisk());

        computer.setCpu(new LenovoCPU());

        computer.setMemory(new LenovoMemory());

        computer.setGraphicsCard(new LenovoGraphicsCard());

        computer.setDisplay(new LenovoDisplay());

        computer.setKeyboard(new LenovoKeyboard());

    }

    public void buildCustomComputer() {//建造混合电脑

        computer.setMotherboard(new HSMotherboard());

        computer.setHardDisk(new LenovoHardDisk());

        computer.setCpu(new LenovoCPU());

        computer.setMemory(new HSMemory());

        computer.setGraphicsCard(new LenovoGraphicsCard());

        computer.setDisplay(new LenvovDisplay());

        computer.setKeyboard(new HSKeyboard());

    }

    public Computer getComputer() {//获得组装好的电脑

        return computer;

    }

}

导演类

//导演类
class Director {

   private ComputerBuilder builder = new ComputerBuilder();

    public void buildHSComputer() {

        // 华硕电脑

        builder.buildHSComputer();

        Computer hsComputer = builder.getComputer();

        hsComputer.showInfo();

    }

    public void buildLenovoComputer() {

        // 联想电脑

        builder.buildLenovoComputer();

        Computer lenovoComputer = builder.getComputer();

        lenovoComputer.showInfo();

    }

    public void buildCustomComputer() {

        // 混合电脑

        builder.buildCustomComputer();

        Computer customComputer = builder.getComputer();

        customComputer.showInfo();

    }

}

上一篇:Laravel的魔法纽带:深入理解Eloquent ORM的关联(Relationships)


下一篇:nvide shortcuts table