构造方法的参数太多,如何解决?

本文深入探讨了Builder模式在解决构造方法参数过多、可读性差等问题的应用,对比了三种创建对象的方法,详细介绍了Builder模式的实现及优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你在写代码的过程中遇到过构造方法的参数太多、构造方法重载太多,而不知道使用哪个方法创建对象的问题吗?或者参数传着传着就传错位置了?

 

比如 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 模式就非常适合使用较多参数构造对象、须保证对象构造后的属性字段安全的场景。

 


【Java面试题与答案】整理推荐

 

 

### 解决Matlab函数输出参数过多的问题 当面对Matlab函数返回过多输出参数的情况时,可以采取多种方法来简化接口并提高代码可读性和维护性。 #### 使用结构体作为输出容器 一种有效的方式是通过结构体封装多个输出值。这不仅减少了函数签名中的参数数量,还使得调用者能够更清晰地理解各个字段的意义[^3]。 ```matlab function result = myFunction(inputParam) % 函数实现... % 将所有输出打包到一个结构体内 result.field1 = value1; result.field2 = value2; result.fieldN = valueN; end ``` #### 应用类对象管理复杂状态 对于更加复杂的场景,考虑创建自定义类来表示一组相互关联的数据成员及其行为。这种方式有助于构建面向对象的设计模式,在保持逻辑连贯的同时减少对外暴露的公共API表面面积。 ```matlab classdef MyClass properties Property1 Property2 PropertyN end methods function obj = MyClass() % 构造器初始化属性... end function update(obj, inputParams) % 更新内部状态的方法... end function displayResults(obj) disp([obj.Property1, obj.Property2]); %#ok<DISP> end end end ``` #### 返回元胞数组或表格变量 如果不同类型的输出之间存在某种自然顺序或者它们本质上就是表格式数据,则可以选择使用`cell`阵列或是`table`类型作为统一载体传递给外部使用者。 ```matlab % 元胞数组方式 function outputs = anotherFunction(inputs) ... outputs{1} = dataA; outputs{2} = dataB; outputs{n} = dataC; end % 表格形式 function tblOutput = yetAnotherFunc(tblInput) ... tblOutput.VarName1 = computedDataSeries1; tblOutput.VarName2 = computedDataSeries2; tblOutput.VarNameM = computedDataSeriesM; end ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值