软件设计之生成器模式

 理解生成器模式在于:一个对象若由多个部分组成,只要构建好这些部分然后拼接到一起就组成了一个完整的对象。比如一台电脑,它的类型可以不一样,可以是苹果的,可以是联想的,等等。同一款电脑它的组件也不一样,有的内存大一点,有的CPU高级点。如果把不同款式的电脑都封装成一个对象的话就比较复杂,使用生成器模式可以快速构建自己需要的对象。下面使用这个案例说明。

软件设计之生成器模式_第1张图片

 软件设计之生成器模式_第2张图片

package Builder;

public class Client {
    public static void main(String[] args) {
        Direct direct = new Direct();
        ComputerBulider computerBuliderA = new ComputerABuilder();
        direct.setCb(computerBuliderA);
        Computer computerA = direct.contructor();
        System.out.println(computerA.toString());
        ComputerBulider computerBuliderB = new ComputerBBuilder();
        direct.setCb(computerBuliderB);
        Computer computerB = direct.contructor();
        System.out.println(computerB.toString());
    }
}
package Builder;

public class Computer {
    private String cpu;//中央处理器
    private String memory;//内存
    private String disk;//硬盘
    private String graphics;//显卡
    private String mainboard;//主板

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getMemory() {
        return memory;
    }

    public void setMemory(String memory) {
        this.memory = memory;
    }

    public String getDisk() {
        return disk;
    }

    public void setDisk(String disk) {
        this.disk = disk;
    }

    public String getGraphics() {
        return graphics;
    }

    public void setGraphics(String graphics) {
        this.graphics = graphics;
    }

    public String getMainboard() {
        return mainboard;
    }

    public void setMainboard(String mainboard) {
        this.mainboard = mainboard;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu='" + cpu + '\'' +
                ", memory='" + memory + '\'' +
                ", disk='" + disk + '\'' +
                ", graphics='" + graphics + '\'' +
                ", mainboard='" + mainboard + '\'' +
                '}';
    }
}
package Builder;

public class ComputerABuilder extends ComputerBulider{
    @Override
    public void buildCpu() {
        computer.setCpu("CPUA");
    }

    @Override
    public void buildMemory() {
        computer.setMemory("内存A");
    }

    @Override
    public void buildDisk() {
        computer.setDisk("硬盘A");
    }

    @Override
    public void buildGraphics() {
        computer.setGraphics("显卡A");
    }

    @Override
    public void buildMainboard() {
        computer.setMainboard("主板A");
    }
}
package Builder;

public class ComputerBBuilder extends ComputerBulider{
    @Override
    public void buildCpu() {
        computer.setCpu("CPUB");
    }

    @Override
    public void buildMemory() {
        computer.setMemory("内存B");
    }

    @Override
    public void buildDisk() {
        computer.setDisk("硬盘B");
    }

    @Override
    public void buildGraphics() {
        computer.setGraphics("显卡B");
    }

    @Override
    public void buildMainboard() {
        computer.setMainboard("主板B");
    }
}
package Builder;

public abstract class ComputerBulider {
    protected Computer computer = new Computer();
    public abstract void buildCpu();
    public abstract void buildMemory();
    public abstract void buildDisk();
    public abstract void buildGraphics();
    public abstract void buildMainboard();
    public Computer getComputer() {
        return computer;
    }
}
package Builder;

public class Direct {
    private ComputerBulider cb;

    public void setCb(ComputerBulider cb) {
        this.cb = cb;
    }
    public Computer contructor() {
        cb.buildDisk();
        cb.buildGraphics();
        cb.buildCpu();
        cb.buildMainboard();
        cb.buildMemory();
        return cb.getComputer();
    }
}

所有的电脑都是一个产品Product,所有的组件都在这里声明。ConcreteBulider作用是构建电脑具体的部件,Direct作用是将部件组装,并提供方法可以让Client拿到电脑。在Client中声明了Direct和ConcreteBulider,将ConcreteBulider传入Direct中完成组装然后调用方法拿到电脑。

你可能感兴趣的:(开发工具,java,开发语言)