likes
comments
collection
share

Java设计模式(三)--建造者模式

作者站长头像
站长
· 阅读数 24

简介

当一个复杂的对象装配时,所需要传入的参数比较多的时候,并且会根据不同的参数来装配对象,当参数不同时,装配的对象的内部也不同,可以使用过建造者模式来解决。

汽车类

public class Car {

    /** 雷达 */
    private String radar;

    /** 压力监控表 */
    private String monitor;

    /** 记录仪 */
    private String recorder;

    /** 倒车影像 */
    private String reversingImage;

    /** 工具箱 */
    private String toolBox;

    public Car(){

    }

    public Car(String radar, String monitor, String recorder, String reversingImage, String toolBox) {
        if(!((null == radar) ? (null == reversingImage) : (null != reversingImage))){
            throw new RuntimeException();
        }
        this.monitor = monitor;
        this.recorder = recorder;
        this.toolBox = toolBox;
    }

    public void setRadar(String radar) {
        if(null == reversingImage){
            throw new RuntimeException();
        }
        this.radar = radar;
    }

    public void setReversingImage(String reversingImage) {
        if(null == radar){
            throw new RuntimeException();
        }
        this.reversingImage = reversingImage;
    }

    public void setMonitor(String monitor) {
        this.monitor = monitor;
    }

    public void setRecorder(String recorder) {
        this.recorder = recorder;
    }

    public void setToolBox(String toolBox) {
        this.toolBox = toolBox;
    }
}

构造方法模式

public void build(){
    Car car = new Car(null,"1","1",null,"1");
}

当我们在购买汽车的时候,需要选配一些配置的时候,并且雷达和倒车影像是需要同时选择的,不能单独选配,此时就需要在构造方法中进行参数的校验,如果说构造方法中的参数比较多的时候,但是汽车中有一些配置我们并不想要,此时就需要在构造方法中传递null,此时构造方法中一些不必要的参数都需要传递就比较麻烦。

set方法模式

public void build(){
    Car car = new Car();
    car.setMonitor("1");
    car.setRecorder("1");
}

set方法模式下,如果说雷达和倒车影像是需要同时选择的,不能单独选配,此时你就会发现如果我先选雷达再选配倒车影像的时候,就会发现先选雷达的时候倒车影像没有选配,先选倒车影像的时候雷达没有选配,这样就会造成互相矛盾。

建造者模式

public class Car {

    private Car(Build build){

    }

      public static class Build {

        /** 雷达 */
        private String radar;

        /** 压力监控表 */
        private String monitor;

        /** 记录仪 */
        private String recorder;

        /** 倒车影像 */
        private String reversingImage;

        /** 工具箱 */
        private String toolBox;

        public Build setRadar(String radar) {
            this.radar = radar;
            return this;
        }

        public Build setMonitor(String monitor) {
            this.monitor = monitor;
            return this;
        }

        public Build setRecorder(String recorder) {
            this.recorder = recorder;
            return this;
        }

        public Build setReversingImage(String reversingImage) {
            this.reversingImage = reversingImage;
            return this;
        }

        public Build setToolBox(String toolBox) {
            this.toolBox = toolBox;
            return this;
        }

        public Car build(){
            if((null == radar) ? (null == reversingImage) : (null != reversingImage)){
                return new Car(this);
            }
            throw new RuntimeException();
        }

    }

}

使用建造者模式的时候,我们需要在Car类中添加一个Build内部类,使用private修饰Car构造方法让其不能被外部访问,并且需要将Car类中的一些变量放置到Build类中,并且Build中的set方法需要返回当前Build对象,这样才能链式访问,最终Build中的build方法来校验参数并返回Car对象,当我们使用建造者模式来创建完对象之后,是不能再去改变Car内部中的一些属性的。

public void build(){
    Car car = new Car.Build().setMonitor("1").setToolBox("1").build();
}

Java设计模式(三)--建造者模式

可以看到当使用建造者模式创建完对象之后,再去调用carset方法修改属性是不被允许的。

总结

建造者模式一般是在创建对象比较复杂的情况下使用,当创建对象比较简单的时候,我们可以用构造方法和set方法的方式来创建即可。

转载自:https://juejin.cn/post/7213285816492113978
评论
请登录