Backend Development 13 min read

Using Strategy Enum to Replace Large if‑else Chains in Java

This article explains how to replace bulky if‑else chains in Java with a lightweight strategy‑enum approach, improving readability, extensibility, and reducing procedural code by leveraging enum‑based strategy pattern for daily task scheduling.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Using Strategy Enum to Replace Large if‑else Chains in Java

The author reflects on early Java OOP programming where many if‑else statements cluttered code, noting that excessive if‑else is a procedural implementation.

When a business requires handling a week’s worth of daily tasks, a long if‑else chain becomes unreadable and redundant.

To solve this, the author recommends using a strategy enum, which offers better readability, extensibility, and a lightweight alternative to factories, classic strategy pattern, or heavy rule engines.

Traditional if‑else implementation for a getToDo(String day) method:

1
//if-else形式判断
2
public
String
getToDo
(String day)
{
3
if
(
"Monday"
.equals(day)){
......省略复杂语句
4
return
"今天上英语课"
;
5
}
else
if
(
"Tuesday"
.equals(day)){
.....省略复杂语句
return
"今天上语文课"
;
7
}
else
if
(
"Wednesday"
.equals(day)){
......省略复杂语句
8
return
"今天上数学课"
;
9
}
else
if
(
"Thursday"
.equals(day)){
......省略复杂语句
10
return
"今天上音乐课"
;
11
}
else
if
(
"sunday"
.equals(day)){
......省略复杂语句
12
return
"今天上编程课"
;
13
}
else
{
14
此处省略
10086
行......
15
}
16
}

This code works for a few cases, but when hundreds of branches are needed it becomes unwieldy.

The refactored approach defines a getToDo method that delegates to a CheckDay class, which calls DayEnum.valueOf(day) and then invokes the enum’s toDo() method.

//策略枚举判断
public
String
getToDo
(String day)
{
CheckDay checkDay=
new
CheckDay();
return
checkDay.day(DayEnum.valueOf(day));
}

Inside CheckDay.day(DayEnum dayEnum) the call simply returns dayEnum.toDo() , delegating the actual work to the enum constant.

public
class
CheckDay {
public
String
day( DayEnum dayEnum) {
return
dayEnum.toDo();
}
}

The DayEnum enum declares an abstract toDo() method and overrides it in each constant (Monday, Tuesday, …), allowing each day to return its specific task string.

public
enum
DayEnum {
Monday {
@Override
public
String
toDo
()
{
......省略复杂语句
return
"今天上英语课"
;
}
},
Tuesday {…},
Wednesday {…},
Thursday {…};
public
abstract
String
toDo
()
;
}

To handle duplicate functionality (e.g., several days sharing the same task), an internal enum Type is introduced; each day stores a Type and delegates its toDo() to the shared implementation.

public
enum
DayEnum {
Monday(
"星期一"
, Type.ENGLISH),
Tuesday(
"星期二"
, Type.ENGLISH),
Wednesday(
"星期三"
, Type.ENGLISH),
Thursday(
"星期四"
, Type.CHINESE);
private final Type type;
private final String day;
DayEnum(String day, Type type) {
this.day = day;
this.type = type;
}
String toDo() {
return type.toDo();
}
private enum Type {
ENGLISH {
@Override
public String toDo() {
......省略复杂语句
return "今天上英语课";
}
},
CHINESE {
@Override
public String toDo() {
......省略复杂语句
return "今天上语文课";
}
};
public abstract String toDo();
}
}

The author concludes that strategy enums combine the strategy pattern with enums, offering a functional‑style solution that decouples complex branching logic, improves maintainability, and behaves like a key‑to‑door mechanism.

At the end, the author promotes his PDF collections on Spring Cloud, Spring Boot, and MyBatis, and asks readers to like, share, and follow his public account.

Backenddesign patternsJavaStrategy Patternenumif-else
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.