如何去掉不够优雅的IF-ELSE

不够优雅的IF-ELSE: 

       在一个方法中根据两个参数的不同值组合来返回四种可能的类型,你可以使用条件语句,例如 if-else 语句或 switch 语句,来实现这个逻辑。以下是一个示例,假设你有两个参数 param1param2

public YourReturnType yourMethod(int param1, int param2) {
    if (param1 == 0 && param2 == 0) {
        // 根据参数1和参数2的组合返回第一种类型
        return FirstTypeValue;
    } else if (param1 == 0 && param2 == 1) {
        // 根据参数1和参数2的组合返回第二种类型
        return SecondTypeValue;
    } else if (param1 == 1 && param2 == 0) {
        // 根据参数1和参数2的组合返回第三种类型
        return ThirdTypeValue;
    } else {
        // 默认情况,返回第四种类型
        return FourthTypeValue;
    }
}

        上述代码固然能够满足我们的需求,但是总感觉不够优雅。

优雅的CODE

a.使用映射表

        创建一个映射表(Map),将参数组合与对应的返回类型关联起来。这可以使你的代码更清晰。

Map<Pair<Integer, Integer>, YourReturnType> map = new HashMap<>();
map.put(new Pair<>(0, 0), FirstTypeValue);
map.put(new Pair<>(0, 1), SecondTypeValue);
map.put(new Pair<>(1, 0), ThirdTypeValue);

YourReturnType result = map.get(new Pair<>(param1, param2));
if (result == null) {
    result = FourthTypeValue; // 默认值
}
return result;

        请注意,这需要引入一个 Pair 类或使用其他方式来表示参数组合。

b.使用映射表

        如果可能的返回类型有限且固定,你可以考虑使用枚举,每个枚举值代表一种返回类型。然后,你可以使用枚举的 valueOf 方法来获取对应的返回类型。

public YourReturnType yourMethod(int param1, int param2) {
    try {
        return YourReturnTypeEnum.valueOf("TYPE_" + param1 + param2);
    } catch (IllegalArgumentException e) {
        return YourReturnTypeEnum.DEFAULT;
    }
}

        这里 YourReturnTypeEnum 是一个枚举,它定义了可能的返回类型,例如 TYPE_00TYPE_01TYPE_10TYPE_11,以及默认的 DEFAULT 类型。

c.使用函数式编程

        如果你使用 Java 8 或更高版本,可以考虑使用函数式编程的方式来处理这个问题。你可以创建一个函数接口,然后根据输入参数的组合返回对应的结果。

interface ResultCalculator {
    YourReturnType calculate(int param1, int param2);
}

Map<Pair<Integer, Integer>, ResultCalculator> calculatorMap = new HashMap<>();
calculatorMap.put(new Pair<>(0, 0), (p1, p2) -> FirstTypeValue);
calculatorMap.put(new Pair<>(0, 1), (p1, p2) -> SecondTypeValue);
// ...

ResultCalculator calculator = calculatorMap.get(new Pair<>(param1, param2));
YourReturnType result = (calculator != null) ? calculator.calculate(param1, param2) : FourthTypeValue;
return result;

d.使用策略模式

        策略模式允许你将不同的计算逻辑封装在不同的策略类中,然后根据参数来选择不同的策略执行。这种方式使代码更具扩展性。

interface CalculationStrategy {
    YourReturnType calculate(int param1, int param2);
}

class StrategyA implements CalculationStrategy {
    @Override
    public YourReturnType calculate(int param1, int param2) {
        // 实现策略A的计算逻辑
    }
}

class StrategyB implements CalculationStrategy {
    @Override
    public YourReturnType calculate(int param1, int param2) {
        // 实现策略B的计算逻辑
    }
}

// 策略选择
Map<Pair<Integer, Integer>, CalculationStrategy> strategyMap = new HashMap<>();
strategyMap.put(new Pair<>(0, 0), new StrategyA());
strategyMap.put(new Pair<>(0, 1), new StrategyB());
// ...

CalculationStrategy strategy = strategyMap.get(new Pair<>(param1, param2));
YourReturnType result = (strategy != null) ? strategy.calculate(param1, param2) : FourthTypeValue;
return result;

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值