Eliminate If‑Else Chains with Enum‑Based Strategy in Java
This article demonstrates how to replace cumbersome if‑else statements with a clean enum‑driven design in Java, using an abstract rule class, concrete channel implementations, and an enhanced enumeration that binds each channel to its processing logic, adhering to the open‑closed principle.
Solution Idea
When data arrives from different sources (e.g., Tencent, Toutiao) and each source requires distinct processing, we first define an abstract class GeneralChannelRule with an abstract process() method that each channel must implement.
public abstract class GeneralChannelRule {
public abstract void process();
}Concrete rule classes implement the specific logic 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
}
}An initial enum simply lists the channel identifiers:
public enum ChannelRuleEnum {
TOUTIAO("TOUTIAO"),
TENCENT("TENCENT");
// ...
}Using this enum in a main method leads to multiple if‑else checks, violating the Open‑Closed Principle because adding a new channel requires modifying the method.
New Approach
We enhance the enum to hold a GeneralChannelRule instance 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 value : ChannelRuleEnum.values()) {
if (value.name.equals(name)) {
return value;
}
}
return null;
}
public String getName() { return name; }
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.channel;
rule.process();
}By binding each channel identifier to its rule implementation within the enum, we eliminate the need for repetitive if‑else blocks. Adding a new channel now only requires creating a new rule class and adding an entry to the enum, preserving existing code and complying with the open‑closed principle.
Conclusion
Using enums to replace if‑else chains offers a clean, maintainable solution; other techniques such as the State pattern can also achieve similar results.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
