Backend Development 5 min read

Replacing If‑Else with Enum‑Based Channel Rules in Java

This article demonstrates how to refactor channel‑specific processing logic in Java by introducing an abstract rule class, concrete channel rule implementations, and an enum that binds channel identifiers to their rule objects, thereby eliminating cumbersome if‑else statements and adhering to the open/closed principle.

Top Architect
Top Architect
Top Architect
Replacing If‑Else with Enum‑Based Channel Rules in Java

The article addresses a common scenario where data from different channels (e.g., Tencent, TouTiao) requires distinct processing logic, and the initial solution uses a series of if‑else checks in the main method.

Solution Idea

1. Define an abstract class GeneralChannelRule with an abstract process() method.

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

2. Implement concrete rule classes for each channel.

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

3. Create a simple enum ChannelRuleEnum that maps channel codes to their rule implementations and provides 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 value : values()) {
            if (value.name.equals(name)) {
                return value;
            }
        }
        return null;
    }
}

4. Refactor the main method to use the enum for rule selection, removing the explicit if‑else chain.

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

Analysis

The original approach violated the Open/Closed Principle because adding a new channel required modifying the main method. By binding channel identifiers to rule objects inside the enum, new channels can be added simply by creating a new rule class and adding a new enum constant, without touching existing code.

Finally, the article notes that using enums to eliminate if‑else is one of many techniques (e.g., State pattern) for achieving cleaner, more maintainable code.

backendJavaenumCodeExampleDesignPatternOpenClosedPrinciple
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login 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.