Effective Java第二条builder有感

有这么一个computer实体类,但是我们不想构造一个computer的时候都用重载构造方法或者set方法,不美观,也不太好扩展,不符合设计原则。所以我们可以通过链式编程的方式来实现自定义computer配置,而且可以通过建造者模式实现固定computer配置。

public class Computer {

    private String cpu;

    private String cuda;

    private String boss;

    private String eletric;

    private String disk;

    private String memory;


    public static class Builder {
		
		// 因为是必要参数,设置为final不可更改
        private final String cpu;

        private final String cuda;
		
		// 这里一定赋予初始值,担心空指针问题。
        private String boss = "";

        private String electric = "";

        private String disk = "";

        private String memory = "";
		
		// 构造函数构造必要的参数
        public Builder(String cpu, String cuda) {
            this.cpu = cpu;
            this.cuda = cuda;
        }

        public Builder setBoss(String boss){
            this.boss = boss;
            return this;
        }

        public Builder setElectric(String electric){
            this.electric = electric;
            return this;
        }

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

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

        public Computer build(){
            return new Computer(this);
        }
    }
	
	// 构造器入参为Builder
    public Computer(Builder builder) {
        this.cpu = builder.cpu;
        this.cuda = builder.cuda;
        this.memory = builder.memory;
        this.disk = builder.disk;
        this.eletric = builder.electric;
        this.boss = builder.boss;
    }

}

测试链式编程,十分美观,间接。

Computer computer = new Computer.Builder("i7", "3090猛禽")
                .setDisk("东芝硬盘")
                .setMemory("三星内存")
                .setBoss("华硕主板")
                .setElectric("比亚迪电源")
                .build();
        System.out.println(computer);

下面加入建造者模式:

public interface IComputerBuilder {

    Computer build();
}

低等配置的电脑

public class LowLevelComputer implements IComputerBuilder {

    private Computer computer = null;

    @Override
    public Computer build() {
        computer = new Computer.Builder("低配i7", "低配3090猛禽")
                .setDisk("低配东芝硬盘")
                .setMemory("低配三星内存")
                .setBoss("低配华硕主板")
                .setElectric("低配比亚迪电源")
                .build();
        return computer;
    }
}

高等配置的电脑

public class HighLevelComputer implements IComputerBuilder{

    private Computer computer = null;

    @Override
    public Computer build() {
        computer = new Computer.Builder("高配i7", "高配3090猛禽")
                .setDisk("高配东芝硬盘")
                .setMemory("高配三星内存")
                .setBoss("高配华硕主板")
                .setElectric("高配比亚迪电源")
                .build();
        return computer;
    }
}

建造器

public class ComputerBuilder {

    public static Computer build(IComputerBuilder builder){
        return builder.build();
    }
}

测试建造者模式:

Computer lowLevelComputer = ComputerBuilder.build(new LowLevelComputer());
        Computer highLevelComputer = ComputerBuilder.build(new HighLevelComputer());

        System.out.println(lowLevelComputer);
        System.out.println(highLevelComputer);

你可能感兴趣的:(设计模式,java,开发语言)