构造方法的参数太多,如何解决?
16lz
2021-01-22
你在写代码的过程中应该也遇到因过构造方法的参数太多、构造方法重载太多,而不知道使用哪个方法创建对象的问题;或者参数传着传着就错位了。
比如 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 方法。
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 对象可以这样创建
Person person = new Person();person.setId(1);person.setName("ConstXiong");person.setSex("男");person.setHeight(1.70);person.setWeight(150.0);
这样做解决了构造方法多个参数和可读性的问题,但是引入了新的问题,就是无法做到属性参数在构造完之后是不可改变的,导致存在数据不安全的问题。
第 3 种方式,使用 Builder 模式。代码如下
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 模式也带了新的问题:
- 创新对象前,必须创建 Builder 对象,多一些性能开销。对性能要求极高的场景下慎用。
- Builder 模式跟 1、2 两种方式比,代码行数更多,显得有点啰嗦。
所以说,软件开发一般没有完美的解决方法,只有不同场景的最优解决办法;一种方法能解决某些问题,必然带来其他问题。
Builder 模式就非常适合使用较多参数构造对象、须保证对象构造后的属性字段安全的场景。
PS:Java 面试题汇总在:这里
更多相关文章
- 从代码的改进,看参数行为化与Lambda
- Spring Ioc 实例化 Bean 对象有几种方式?
- PHP-FPM参数调优
- Javascript面向对象入门
- springboot 读写 session 交互参数
- HTTP触发Jenkins参数化构建
- 如何在 Java 中构造对象(学习 Java 编程语言 034)
- 我就改了一下参数,竟然让Tomcat和JVM的性能提升了两倍!