巧妙替代 if - else 的方案之枚举

场景:当我们的项目中需要接收到一些数据并且需要对其进行处理时,由于它们来自于不同的渠道(如:阿里、腾讯),不同渠道所需要的处理方式不同,下面我们写一个常见的 Demo 来实现该场景。

1、首先,构建一个基础规则的抽象类 GeneralChannelRule,定义一个抽象方法 process(),不同的渠道都需要实现该抽象方法。

public abstract class GeneralChannelRule {

    public abstract void process();

}

2、建立一个简单的枚举类。

public enum ChannelRuleEnum {

    ALIBABA("Alibaba"),
    TENCENT("Tencent");

    private String code;

    ChannelRuleEnum(String channel) {
        this.code = channel;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

3、定义一个阿里的规则类,用来处理对于阿里渠道数据的具体处理逻辑。

public class AlibabaChannelRule extends GeneralChannelRule {

    @Override
    public void process() {
        // Alibaba 数据处理逻辑
        System.out.println("This is Alibaba");
    }
}

4、定义一个腾讯的规则类,用来处理对于腾讯渠道数据的具体处理逻辑。

public class TencentChannelRule extends GeneralChannelRule {

    @Override
    public void process() {
        // Tencent 数据处理逻辑
        System.out.println("This is Tencent");
    }
}

5、使用规则对数据进行处理。

public class Demo {

    public static void main(String[] args) {
        // 模拟接收到的数据是来自 Alibaba
        String channel = "Alibaba";
        GeneralChannelRule rule = null;
        if (ChannelRuleEnum.ALIBABA.getCode().equals(channel)) {
            rule = new AlibabaChannelRule();
        } else if (ChannelRuleEnum.TENCENT.getCode().equals(channel)) {
            rule = new TencentChannelRule();
        } else {
            // 无匹配项
        }
        // 执行逻辑
        rule.process();
    }
}

6、如图为实现结果。

通过如上的方式,能够实现需求。

但是存在两个缺点:

其一、当我们需要新增新的渠道的时候,需要对 main 方法中的逻辑进行修改调整。这一点违背了设计模式中的开放封闭规则。开放封闭规则的核心思想是软件实体是可扩展而不可修改的。也就是说,对扩展是开放的,而对修改则是封闭的。

其二、新增渠道后,修改代码会产生大量的 if - else,不太优雅。

为了解决以上的两个问题,我们可以借助枚举类来进行优化。

1、调整一下枚举类,增加一个属性 GeneralChannelRule,且给对应的渠道构建对应的实现类 GeneralChannelRule,同时新增一个匹配方法 match()。 

public enum ChannelRuleEnum {

    ALIBABA("Alibaba", new AlibabaChannelRule()),

    TENCENT("Tencent", new TencentChannelRule());

    private String code;

    private GeneralChannelRule channelRule;

    /**
     * 匹配
     * @param code 渠道值
     * @return
     */
    public static ChannelRuleEnum match(String code){
        ChannelRuleEnum[] values = ChannelRuleEnum.values();
        for (ChannelRuleEnum value : values) {
            if(value.code.equals(code)){
                return value;
            }
        }
        return null;
    }

    ChannelRuleEnum(String channel, GeneralChannelRule channelRule) {
        this.code = channel;
        this.channelRule = channelRule;
    }

    public GeneralChannelRule getChannelRule() {
        return channelRule;
    }

    public void setChannel(GeneralChannelRule channelRule) {
        this.channelRule = channelRule;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

2、改写程序。

public class Demo {

    public static void main(String[] args) {
        // 模拟接收到的数据是来自 Tencent
        String channel = "Tencent";
        ChannelRuleEnum channelRule = ChannelRuleEnum.match(channel);
        GeneralChannelRule rule = channelRule.getChannelRule();
        rule.process();
    }
}

3、如下为实现结果。

通过使用枚举类,在枚举中将 key 与渠道规则具体实现进行绑定。通过优化,可以减少 if - else 使得代码更加优雅。同时,如果需要新增渠道,还需要做两步操作即可完成。这样就不需要改动原有的代码逻辑,符合了开放封闭原则。 操作步骤如下:

  1. 定义新增渠道实现类,继承 GeneralChannelRule 抽象类,且实现其抽象方法 process()。
  2. 在枚举类 ChannelRuleEnum 新增对应的渠道实现类。

以上就是通过枚举类的方式来巧妙替代 if - else 的方案,对于替代 if - else 的优秀解决方案还有很多(如:策略模式、工厂模式等),感兴趣的朋友可以查阅相关的资料。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值