You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何消除Java中的多重if语句?优化多参数构造方法

优化多参数Java构造方法的方案

针对你这段构造方法,我们可以从消除冗余代码、避免浮点数坑、提升可读性三个维度来优化,下面是具体方案:


1. 先解决浮点数相等判断的隐患

直接用==比较double类型虽然对0.5这种精确浮点数有效,但对于其他可能存在精度丢失的浮点数(比如0.1)会出问题。建议用Double.compare()或者阈值判断来替代:

// 用Double.compare判断相等,避免精度问题
private double resolveCpuValue(double input, double full) {
    return Double.compare(input, 0.5) == 0 ? full : input;
}

// 若参数可能存在精度误差,用阈值判断
private double resolveCpuValue(double input, double full) {
    return Math.abs(input - 0.5) < 1e-9 ? full : input;
}

2. 提取重复逻辑,消除冗余if-else

四个CPU参数的处理逻辑完全一致,把这段逻辑封装成私有方法后,构造方法里的代码会大幅简化:

public Latency(final double full, final double cpuOne, final double cpuTwo, final double cpuThree, final double cpuFour) {
    // 校验full范围,明确异常信息方便调试
    if (full <= 0.0 || full > 10.0) {
        throw new IllegalArgumentException("full必须大于0且不超过10.0,当前值:" + full);
    }
    this.full = full;

    // 校验CPU参数不能全为0
    if (cpuOne == 0 && cpuTwo == 0 && cpuThree == 0 && cpuFour == 0) {
        throw new IllegalArgumentException("CPU延迟参数不能全为0");
    }

    // 调用统一方法处理赋值
    this.cpuOne = resolveCpuValue(cpuOne, full);
    this.cpuTwo = resolveCpuValue(cpuTwo, full);
    this.cpuThree = resolveCpuValue(cpuThree, full);
    this.cpuFour = resolveCpuValue(cpuFour, full);
}

// 封装重复逻辑
private double resolveCpuValue(double input, double full) {
    return Double.compare(input, 0.5) == 0 ? full : input;
}

3. 优化多参数问题,避免传参错误

原构造方法有5个参数,容易出现参数顺序传错的问题,推荐两种优化方式:

方式一:使用可变参数

如果CPU参数的数量固定为4,可以用可变参数简化传参(同时要校验参数数量):

public Latency(final double full, final double... cpuValues) {
    // 校验full范围
    if (full <= 0.0 || full > 10.0) {
        throw new IllegalArgumentException("full必须大于0且不超过10.0,当前值:" + full);
    }
    this.full = full;

    // 校验CPU参数数量必须为4
    if (cpuValues == null || cpuValues.length != 4) {
        throw new IllegalArgumentException("必须传入4个CPU延迟参数");
    }

    // 校验是否全为0
    boolean allZero = true;
    for (double val : cpuValues) {
        if (val != 0) {
            allZero = false;
            break;
        }
    }
    if (allZero) {
        throw new IllegalArgumentException("CPU延迟参数不能全为0");
    }

    // 批量赋值
    this.cpuOne = resolveCpuValue(cpuValues[0], full);
    this.cpuTwo = resolveCpuValue(cpuValues[1], full);
    this.cpuThree = resolveCpuValue(cpuValues[2], full);
    this.cpuFour = resolveCpuValue(cpuValues[3], full);
}

方式二:使用建造者模式(更推荐)

建造者模式可以让对象构造过程更清晰,完全避免参数顺序错误的问题:

public class Latency {
    private final double full;
    private final double cpuOne;
    private final double cpuTwo;
    private final double cpuThree;
    private final double cpuFour;

    // 私有构造方法,只能通过Builder创建
    private Latency(Builder builder) {
        // 校验逻辑
        if (builder.full <= 0.0 || builder.full > 10.0) {
            throw new IllegalArgumentException("full必须大于0且不超过10.0,当前值:" + builder.full);
        }
        if (builder.cpuOne == 0 && builder.cpuTwo == 0 && builder.cpuThree == 0 && builder.cpuFour == 0) {
            throw new IllegalArgumentException("CPU延迟参数不能全为0");
        }

        this.full = builder.full;
        this.cpuOne = resolveCpuValue(builder.cpuOne, builder.full);
        this.cpuTwo = resolveCpuValue(builder.cpuTwo, builder.full);
        this.cpuThree = resolveCpuValue(builder.cpuThree, builder.full);
        this.cpuFour = resolveCpuValue(builder.cpuFour, builder.full);
    }

    private double resolveCpuValue(double input, double full) {
        return Double.compare(input, 0.5) == 0 ? full : input;
    }

    // 静态内部Builder类
    public static class Builder {
        private double full;
        private double cpuOne = 0;
        private double cpuTwo = 0;
        private double cpuThree = 0;
        private double cpuFour = 0;

        public Builder full(double full) {
            this.full = full;
            return this;
        }

        public Builder cpuOne(double cpuOne) {
            this.cpuOne = cpuOne;
            return this;
        }

        public Builder cpuTwo(double cpuTwo) {
            this.cpuTwo = cpuTwo;
            return this;
        }

        public Builder cpuThree(double cpuThree) {
            this.cpuThree = cpuThree;
            return this;
        }

        public Builder cpuFour(double cpuFour) {
            this.cpuFour = cpuFour;
            return this;
        }

        public Latency build() {
            return new Latency(this);
        }
    }
}

调用示例:

// 构造过程清晰,不会传错参数
Latency latency = new Latency.Builder()
        .full(8.0)
        .cpuOne(0.5)
        .cpuTwo(3.0)
        .cpuThree(0)
        .cpuFour(2.5)
        .build();

内容的提问来源于stack exchange,提问作者anyonymousgamer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 21:01:04