好比 Person 类,包含 id、姓名、性别、身高、体重属性。为了方便建立对象,咱们通常使用全部属性做为参数提供一个最全的构造方法,而后按需提供部分属性的若干构造方法。代码以下:面试
package constxiong.interview.design;
/** * 对象人 * @author ConstXiong */public class Person {
/** * id */ private final int id; /** * 姓名 */ private final String name; /** * 性别 */ private final String sex; /** * 身高 */ private final Double height; /** * 体重 */ private final Double weight; public Person(int id, String name) { this(id, name, null); } public Person(int id, String name, String sex) { this(id, name, sex, null); } public Person(int id, String name, String sex, Double height) { this(id, name, sex, height, null); }
public Person(int id, String name, String sex, Double height, Double weight) { this.id = id; this.name = name; this.sex = sex; this.height = height; this.weight = weight; }}
当咱们须要建立一个只知道 id、姓名、性别的对象时,调用第 2 个构造方法便可安全
Person person = new Person(1, "ConstXiong", "男");
这样作存在问题:示例中才 5 个参数,实际开发中随着参数的增多,建立对象会愈来愈困难,须要肯定参数的数量与构造方法中的位置;代码可读性不好;容易参数颠倒且很难排查。
为解决上述问题,会有第 2 种作法,提供默认构造方法与属性的 set 方法。session
package constxiong.interview.design;
/** * 对象人 * @author ConstXiong */public class Person {
/** * id */ private int id; /** * 姓名 */ private String name; /** * 性别 */ private String sex; /** * 身高 */ private Double height; /** * 体重 */ private Double weight; public Person() { }
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setSex(String sex) { this.sex = sex; }
public void setHeight(Double height) { this.height = height; }
public void setWeight(Double weight) { this.weight = weight; }}
这种的方式 Person 对象能够这样建立app
Person person = new Person();person.setId(1);person.setName("ConstXiong");person.setSex("男");person.setHeight(1.70);person.setWeight(150.0);
这样作解决了构造方法多个参数和可读性的问题,可是引入了新的问题,就是没法作到属性参数在构造完以后是不可改变的,致使存在数据不安全的问题。
第 3 种方式,使用 Builder 模式。代码以下ide
package constxiong.interview.design;
/** * 对象人 * @author ConstXiong */public class Person {
/** * id */ private final int id; /** * 姓名 */ private final String name; /** * 性别 */ private final String sex; /** * 身高 */ private final Double height; /** * 体重 */ private final Double weight; public static class Builder { private int id; private String name; private String sex; private Double height; private Double weight; public Builder() { } public Builder id(int id) { this.id = id; return this; } public Builder name(String name) { this.name = name; return this; } public Builder sex(String sex) { this.sex = sex; return this; } public Builder height(Double height) { this.height = height; return this; } public Builder weight(Double weight) { this.weight = weight; return this; } public Person build() { return new Person(this); } } private Person(Builder builder) { this.id = builder.id; this.name = builder.name; this.sex = builder.sex; this.height = builder.height; this.weight = builder.weight; }}
建立 Person 对象的代码性能
Person person = new Person.Builder() .id(1) .name("ConstXiong") .sex("男") .height(1.70) .weight(150.0) .build();
Builder 模式须要注意是,Builder 类是静态内部类、类的构造方法是 private 的且参数为 Builder 对象。
Builder 模式不只能够解决构造过程数据安全、参数过多、可读性的问题,还能够自动填充参数、生成对象前对参数之间的关系进行合法校验等...
固然 Builder 模式也带了新的问题:ui
因此说,软件开发通常没有完美的解决方法,只有不一样场景的最优解决办法;一种方法能解决某些问题,必然带来其余问题。
Builder 模式就很是适合使用较多参数构造对象、须保证对象构造后的属性字段安全的场景。
PS:Java 面试题汇总在:这里this