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.
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:
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px"> <span style="color: #1c00cf; line-height: 26px">1</span> <span style="color: #007400; line-height: 26px">//if-else形式判断</span><br/> <span style="color: #1c00cf; line-height: 26px">2</span> <span style="line-height: 26px"><span style="color: #aa0d91; line-height: 26px">public</span> String <span style="color: #1c00cf; line-height: 26px">getToDo</span><span style="color: #5c2699; line-height: 26px">(String day)</span></span>{<br/> <span style="color: #1c00cf; line-height: 26px">3</span> <span style="color: #aa0d91; line-height: 26px">if</span>(<span style="color: #c41a16; line-height: 26px">"Monday"</span>.equals(day)){<br/> ......省略复杂语句<br/> <span style="color: #1c00cf; line-height: 26px">4</span> <span style="color: #aa0d91; line-height: 26px">return</span> <span style="color: #c41a16; line-height: 26px">"今天上英语课"</span>;<br/> <span style="color: #1c00cf; line-height: 26px">5</span> }<span style="color: #aa0d91; line-height: 26px">else</span> <span style="color: #aa0d91; line-height: 26px">if</span>(<span style="color: #c41a16; line-height: 26px">"Tuesday"</span>.equals(day)){<br/> .....省略复杂语句<br/> <span style="color: #aa0d91; line-height: 26px">return</span> <span style="color: #c41a16; line-height: 26px">"今天上语文课"</span>;<br/> <span style="color: #1c00cf; line-height: 26px">7</span> }<span style="color: #aa0d91; line-height: 26px">else</span> <span style="color: #aa0d91; line-height: 26px">if</span>(<span style="color: #c41a16; line-height: 26px">"Wednesday"</span>.equals(day)){<br/> ......省略复杂语句<br/> <span style="color: #1c00cf; line-height: 26px">8</span> <span style="color: #aa0d91; line-height: 26px">return</span> <span style="color: #c41a16; line-height: 26px">"今天上数学课"</span>;<br/> <span style="color: #1c00cf; line-height: 26px">9</span> }<span style="color: #aa0d91; line-height: 26px">else</span> <span style="color: #aa0d91; line-height: 26px">if</span>(<span style="color: #c41a16; line-height: 26px">"Thursday"</span>.equals(day)){<br/> ......省略复杂语句<br/><span style="color: #1c00cf; line-height: 26px">10</span> <span style="color: #aa0d91; line-height: 26px">return</span> <span style="color: #c41a16; line-height: 26px">"今天上音乐课"</span>;<br/><span style="color: #1c00cf; line-height: 26px">11</span> }<span style="color: #aa0d91; line-height: 26px">else</span> <span style="color: #aa0d91; line-height: 26px">if</span>(<span style="color: #c41a16; line-height: 26px">"sunday"</span>.equals(day)){<br/> ......省略复杂语句<br/><span style="color: #1c00cf; line-height: 26px">12</span> <span style="color: #aa0d91; line-height: 26px">return</span> <span style="color: #c41a16; line-height: 26px">"今天上编程课"</span>;<br/><span style="color: #1c00cf; line-height: 26px">13</span> }<span style="color: #aa0d91; line-height: 26px">else</span>{<br/><span style="color: #1c00cf; line-height: 26px">14</span> 此处省略<span style="color: #1c00cf; line-height: 26px">10086</span>行......<br/><span style="color: #1c00cf; line-height: 26px">15</span> }<br/><span style="color: #1c00cf; line-height: 26px">16</span> }<br/></code>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.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px"><span style="color: #007400; line-height: 26px">//策略枚举判断</span><br/><span style="line-height: 26px"><span style="color: #aa0d91; line-height: 26px">public</span> String <span style="color: #1c00cf; line-height: 26px">getToDo</span><span style="color: #5c2699; line-height: 26px">(String day)</span></span>{<br/> CheckDay checkDay=<span style="color: #aa0d91; line-height: 26px">new</span> CheckDay();<br/> <span style="color: #aa0d91; line-height: 26px">return</span> checkDay.day(DayEnum.valueOf(day));<br/>}<br/></code>Inside CheckDay.day(DayEnum dayEnum) the call simply returns dayEnum.toDo(), delegating the actual work to the enum constant.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px"><span style="color: #aa0d91; line-height: 26px">public</span> <span style="color: #aa0d91; line-height: 26px">class</span> CheckDay {<br/> <span style="color: #aa0d91; line-height: 26px">public</span> <span style="color: #5c2699; line-height: 26px">String</span> day( DayEnum dayEnum) {<br/> <span style="color: #aa0d91; line-height: 26px">return</span> dayEnum.toDo();<br/> }<br/>}<br/></code>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.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px"><span style="color: #aa0d91; line-height: 26px">public</span> <span style="color: #aa0d91; line-height: 26px">enum</span> DayEnum {<br/> Monday {<br/> <span style="color: #643820; line-height: 26px">@Override</span><br/> <span style="line-height: 26px"><span style="color: #aa0d91; line-height: 26px">public</span> String <span style="color: #1c00cf; line-height: 26px">toDo</span><span style="color: #5c2699; line-height: 26px">()</span> </span>{<br/> ......省略复杂语句<br/> <span style="color: #aa0d91; line-height: 26px">return</span> <span style="color: #c41a16; line-height: 26px">"今天上英语课"</span>;<br/> }<br/> },<br/> Tuesday {…},<br/> Wednesday {…},<br/> Thursday {…};<br/> <span style="line-height: 26px"><span style="color: #aa0d91; line-height: 26px">public</span> <span style="color: #aa0d91; line-height: 26px">abstract</span> String <span style="color: #1c00cf; line-height: 26px">toDo</span><span style="color: #5c2699; line-height: 26px">()</span></span>;<br/>}<br/></code>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.
<code style="padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px"><span style="color: #aa0d91; line-height: 26px">public</span> <span style="color: #aa0d91; line-height: 26px">enum</span> DayEnum {<br/> Monday(<span style="color: #c41a16; line-height: 26px">"星期一"</span>, Type.ENGLISH),<br/> Tuesday(<span style="color: #c41a16; line-height: 26px">"星期二"</span>, Type.ENGLISH),<br/> Wednesday(<span style="color: #c41a16; line-height: 26px">"星期三"</span>, Type.ENGLISH),<br/> Thursday(<span style="color: #c41a16; line-height: 26px">"星期四"</span>, Type.CHINESE);<br/> private final Type type;<br/> private final String day;<br/> DayEnum(String day, Type type) {<br/> this.day = day;<br/> this.type = type;<br/> }<br/> String toDo() {<br/> return type.toDo();<br/> }<br/> private enum Type {<br/> ENGLISH {<br/> @Override<br/> public String toDo() {<br/> ......省略复杂语句<br/> return "今天上英语课";<br/> }<br/> },<br/> CHINESE {<br/> @Override<br/> public String toDo() {<br/> ......省略复杂语句<br/> return "今天上语文课";<br/> }<br/> };<br/> public abstract String toDo();<br/> }<br/>}<br/></code>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.
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.
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
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.
