How to Replace Massive if‑else Chains in Java with Strategy Enums
This article explains why long if‑else chains in Java OOP are problematic and demonstrates how to use a strategy enum to encapsulate conditional logic, improving readability, extensibility, and reducing boilerplate code in backend applications.
When beginners start with Java object‑oriented programming, they often write long chains of if‑else statements for simple decision logic, which quickly becomes unreadable and hard to maintain.
Although patterns such as Factory or Strategy can replace them, they sometimes feel heavyweight because they introduce many extra classes.
A lightweight alternative is to use a strategy enum that embeds the strategy directly in an enum, providing better readability and extensibility.
Example with if‑else
//if‑else形式判断
public String getToDo(String day) {
if ("Monday".equals(day)) {
// ...省略复杂语句
return "今天上英语课";
} else if ("Tuesday".equals(day)) {
// ...省略复杂语句
return "今天上语文课";
} else if ("Wednesday".equals(day)) {
// ...省略复杂语句
return "今天上数学课";
} else if ("Thursday".equals(day)) {
// ...省略复杂语句
return "今天上音乐课";
} else if ("sunday".equals(day)) {
// ...省略复杂语句
return "今天上编程课";
} else {
// 省略大量代码
}
}This works for a few cases but becomes cumbersome when the number of conditions grows.
Replacing with strategy enum
//策略枚举判断
public String getToDo(String day) {
CheckDay checkDay = new CheckDay();
return checkDay.day(DayEnum.valueOf(day));
}The method converts the input string to a DayEnum value and delegates to dayEnum.toDo(), which each enum constant implements.
public class CheckDay {
public String day(DayEnum dayEnum) {
return dayEnum.toDo();
}
} public enum DayEnum {
Monday {
@Override
public String toDo() {
// ...省略复杂语句
return "今天上英语课";
}
},
Tuesday {
@Override
public String toDo() {
// ...省略复杂语句
return "今天上语文课";
}
},
Wednesday {
@Override
public String toDo() {
// ...省略复杂语句
return "今天上数学课";
}
},
Thursday {
@Override
public String toDo() {
// ...省略复杂语句
return "今天上音乐课";
}
};
public abstract String toDo();
}Each enum constant overrides the abstract toDo() method, similar to subclasses overriding a parent method, allowing the enum to act as a strategy holder.
Adding new cases only requires adding a new enum constant with its implementation, without changing the client code.
Handling duplicate logic
public String getToDoByIfElse(String day) {
if ("Monday".equals(day) || "Tuesday".equals(day) || "Wednesday".equals(day)) {
// ...省略复杂语句
return "今天上英语课";
} else if ("Thursday".equals(day)) {
// ...
}
}With a strategy enum, the same functionality can be expressed by mapping multiple inputs to a single enum constant, eliminating redundancy.
In summary, strategy enums provide a functional‑style, concise way to eliminate massive if‑else blocks in Java backend code, improving readability, extensibility, and maintainability.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
