Using Enums to Eliminate If‑Else Chains in Java Channel Processing
The article demonstrates how to replace cumbersome if‑else statements with a clean enum‑based design in Java, showing abstract rule classes, concrete channel implementations, and a matching method that adheres to the Open‑Closed Principle for scalable data processing across multiple channels.
When data arrives from different sources (e.g., Tencent, Toutiao), each channel requires its own processing logic. The initial solution defines an abstract GeneralChannelRule class with an abstract process() method, then creates concrete rule classes such as TencentChannelRule and TouTiaoChannelRule that implement the specific logic.
public abstract class GeneralChannelRule {
public abstract void process();
} public class TencentChannelRule extends GeneralChannelRule {
@Override
public void process() {
// Tencent processing logic
}
} public class TouTiaoChannelRule extends GeneralChannelRule {
@Override
public void process() {
// TouTiao processing logic
}
}An enum ChannelRuleEnum is then used to map channel identifiers to string codes, but the main method still contains a series of if‑else checks to select the appropriate rule, violating the Open‑Closed Principle and leading to cumbersome code when new channels are added.
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
}
rule.process();
}To resolve these issues, the enum is redesigned to hold both the channel code and a ready‑made instance of the corresponding rule, plus a match() method that returns the matching enum value.
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;
}
public GeneralChannelRule getChannel() {
return channel;
}
}The main method becomes concise and extensible:
public static void main(String[] args) {
String sign = "TOUTIAO";
ChannelRuleEnum channelRule = ChannelRuleEnum.match(sign);
GeneralChannelRule rule = channelRule.getChannel();
rule.process();
}By binding each channel directly to its rule implementation within the enum, the code eliminates repetitive if‑else blocks and adheres to the Open‑Closed Principle: new channels only require adding a new enum constant and rule class, without modifying existing logic. The article concludes by noting that other patterns, such as the State pattern, can also achieve similar reductions in conditional complexity.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.