Java设计模式之建造者模式

为什么需要建造者模式

其实从定义来说建造者模式,似乎并不太好描述,但是如果从实际需求来解释会相对很容易,建造模式主要用于对象的创建。一般创建一些简单的对象我们直接使用 new 就可以了,但是实际开发中一些对象的创建是否真的 只用 new就可以解决呢?

案例

我们来看看在复杂的业务中对象的创建。
假设我们现在要创建一个学生类,但是要通过另一个学生类去获取相关属性,并且会对相关属性做限制,比如年龄不能小于0

不用建造者模式大致可能是这样的

public class Student{
     

private String name;
private String source;
private Integer age;
private String iphone;
private String address;

public Student(String name, String source, Integer age, String iphone, String address) {
     
        // 校验逻辑
        if (age < 0) {
     
            throw new IllegalArgumentException("...");
        }
        this.name = name;
        this.source = source;
        this.age = age;
        this.iphone = iphone;
        this.address = address;
    }

}


现在有一个 StudentBack 类,需要将这个类的一些属性赋值到 Student,暂时想不到合理的业务场景,姑且是这样吧

@Data
public class StudentBack {
     

    private String userName;
    private String userSource;
    private Integer userAge;
    private String userIphone;
    private String userAddress;
}

这里我故意将一些属性设置为不一样,是因为应对不能用BeanCopy

不用建造者模式的一般编程方式

		StudentBack s = new StudentBack();
        Student student = new Student(s.getUserName(), s.getUserSource(), s.getUserAge(), s.getUserIphone(), null);

这里可以看到 初始化类的时候属性一大堆,而且特别容易错,顺序一大堆,如果是增加更多的属性,那代码是不堪设想,你们应该有简单这种类似的代码,有的人说可以用set方法来处理

		StudentBack s = new StudentBack();
        Student student = new Student();
        student.setName(s.getUserName());
        student.setAddress(s.getUserAddress());
        student.setAge(s.getUserAge());
        student.setIphone(s.getUserIphone());
        student.setSource(s.getUserSource());

然后一些参数校验就会放到set方法里面,这样可以看到代码也相对来说有些臃肿
其实也存在一些问题:

  • 如果想要对象再创建后某些属性不能被更改,就不应该暴露set方法
  • 如果一些对象有关联性限制,也无法处理
  • 对象存在无效状态 比如 student.setName(s.getUserName());

使用建造者模式改造

public class Student {
     

    private String name;
    private String source;
    private Integer age;
    private String iphone;
    private String address;

    private Student(Builder builder) {
     
        this.name = builder.name;
        this.source = builder.source;
        this.age = builder.age;
        this.iphone = builder.iphone;
        this.address = builder.address;
    }


    public static  class Builder{
     
        private String name;
        private String source;
        private Integer age;
        private String iphone;
        private String address;

        public Builder setName(String name) {
     
            this.name = name;
            return this;
        }

        public Builder setSource(String source) {
     
            this.source = source;
            return this;
        }

        public Builder setAge(Integer age) {
     
            this.age = age;
            return this;
        }

        public Builder setIphone(String iphone) {
     
            this.iphone = iphone;
            return this;
        }

        public Builder setAddress(String address) {
     
            this.address = address;
            return this;
        }

        public Student builder() {
     
            // 校验逻辑
            if (age < 0) {
     
                throw new  IllegalArgumentException("..");
            }
            // ....

            return new Student(this);
        }



    }

这里可以看到我们私有化了Student的构造方法,添加了静态内部类 Builder , 用于构造 Student

  • 使用
		StudentBack s = new StudentBack();
        Student student = new Student.Builder()
                .setAge(s.getUserAge())
                .setAddress(s.getUserAddress())
                .setIphone(s.getUserIphone())
                .setName(s.getUserName())
                .setSource(s.getUserSource())
                .builder();

相信你用过的三方jar,见过很多这种创建对象的方式,这样就解决了上面出现的一些问题

缺点

可以看到建造者模式中代码是有点重复的,在Student中的属性,又需要在Builder中重复构建

你可能感兴趣的:(JAVA设计模式,设计模式)