Eliminate If‑Else Chains with Enums: A Clean Java Design

This article demonstrates how to replace cumbersome if‑else statements with a robust enum‑based solution in Java, using an abstract rule class and concrete channel implementations to adhere to the open‑closed principle and simplify adding new data sources.

Programmer DD
Programmer DD
Programmer DD
Eliminate If‑Else Chains with Enums: A Clean Java Design

Solution Overview

When data arrives from different channels (e.g., Tencent, TouTiao), each requires distinct processing logic. A simple demo shows how to handle this scenario.

Step 1: Define an abstract rule

public abstract class GeneralChannelRule {
    public abstract void process();
}

Step 2: Implement Tencent rule

public class TencentChannelRule extends GeneralChannelRule {
    @Override
    public void process() {
        // Tencent processing logic
    }
}

Step 3: Implement TouTiao rule

public class TouTiaoChannelRule extends GeneralChannelRule {
    @Override
    public void process() {
        // TouTiao processing logic
    }
}

Step 4: Simple enum for channel selection

public enum ChannelRuleEnum {
    TOUTIAO("TOUTIAO"),
    TENCENT("TENCENT");
    public String code;
    ChannelRuleEnum(String code) { this.code = code; }
}

Step 5: Use the rule to process data

public static void main(String[] args) {
    String sign = "TOUTIAO"; // simulated incoming data channel
    GeneralChannelRule rule;
    if (ChannelRuleEnum.TENCENT.code.equals(sign)) {
        rule = new TencentChannelRule();
    } else if (ChannelRuleEnum.TOUTIAO.code.equals(sign)) {
        rule = new TouTiaoChannelRule();
    } else {
        // no match
        return;
    }
    rule.process();
}

This approach has two drawbacks: adding a new channel requires modifying the main method, violating the Open‑Closed Principle, and it leads to many if‑else statements.

Improved Approach

We enhance the enum to bind each channel with its concrete rule and provide a match method.

public enum ChannelRuleEnum {
    TOUTIAO("TOUTIAO", new TouTiaoChannelRule()),
    TENCENT("TENCENT", new TencentChannelRule());
    public String name;
    public GeneralChannelRule channel;
    ChannelRuleEnum(String name, GeneralChannelRule channel) {
        this.name = name;
        this.channel = channel;
    }
    public static ChannelRuleEnum match(String name) {
        for (ChannelRuleEnum v : values()) {
            if (v.name.equals(name)) return v;
        }
        return null;
    }
    public GeneralChannelRule getChannel() { return channel; }
}

Rewrite the program:

public static void main(String[] args) {
    String sign = "TOUTIAO";
    ChannelRuleEnum channelRule = ChannelRuleEnum.match(sign);
    GeneralChannelRule rule = channelRule.getChannel();
    rule.process();
}

By binding each enum constant to its rule implementation, adding a new channel only requires creating a new rule class and adding a constant to the enum, without touching existing code, thus respecting the Open‑Closed Principle.

Conclusion

Using enums to eliminate if‑else chains provides a clean, extensible solution; other techniques such as the State pattern can also achieve similar results.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaenumbackend-developmentif-elsedesign-patternsopen-closed-principle
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.