Modern Conditional Strategies in Java
Classic Conditionals:
if
if else
if - else if - else
switch
- Ternary operator (
condition ? a : b
)
Advanced or Alternative Structures:
Map<K, Runnable>
orMap<K, Function>
– to simulate switch/if chains for behaviorenum
with behavior (Strategy Pattern) – enums can define a method differently for each constant- Polymorphism – define different implementations for an interface instead of conditional logic
- Command Pattern – store commands in a map or list and execute dynamically
- Chain of Responsibility Pattern – each handler decides whether to handle or pass along
- Reflection – dynamically invoke methods (not recommended for everyday logic, but possible)
- Pattern Matching for
switch
(Java 21+) – powerful new switch functionality with types
Now, let’s write a class that demonstrates a few of the more interesting and non-obvious ones: using enum with behavior, map of commands, and polymorphism.
📦 ConditionalStructuresShowcase.java
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public class ConditionalStructuresShowcase {
// Using Enum with Strategy Pattern
enum Operation {
ADD {
@Override
int apply(int a, int b) {
return a + b;
}
},
SUBTRACT {
@Override
int apply(int a, int b) {
return a - b;
}
},
MULTIPLY {
@Override
int apply(int a, int b) {
return a * b;
}
},
DIVIDE {
@Override
int apply(int a, int b) {
return b != 0 ? a / b : 0;
}
};
abstract int apply(int a, int b);
}
// Using Map of commands
private final Map<String, Runnable> commandMap = new HashMap<>();
public ConditionalStructuresShowcase() {
commandMap.put("hello", () -> System.out.println("Hello there!"));
commandMap.put("goodbye", () -> System.out.println("Goodbye, friend!"));
commandMap.put("surprise", () -> System.out.println("🎉 Surprise! 🎉"));
}
public void executeCommand(String command) {
commandMap.getOrDefault(command, () -> System.out.println("Unknown command")).run();
}
// Using Polymorphism to avoid if-else
interface Animal {
void speak();
}
static class Dog implements Animal {
public void speak() {
System.out.println("Woof!");
}
}
static class Cat implements Animal {
public void speak() {
System.out.println("Meow!");
}
}
static class NinjaTurtle implements Animal {
public void speak() {
System.out.println("Cowabunga!");
}
}
public static void main(String[] args) {
ConditionalStructuresShowcase showcase = new ConditionalStructuresShowcase();
// Example: enum with strategy
System.out.println("Enum ADD: " + Operation.ADD.apply(5, 3));
System.out.println("Enum DIVIDE: " + Operation.DIVIDE.apply(10, 2));
// Example: map of commands
showcase.executeCommand("hello");
showcase.executeCommand("surprise");
showcase.executeCommand("not-a-command");
// Example: polymorphism
Animal dog = new Dog();
Animal cat = new Cat();
Animal turtle = new NinjaTurtle();
dog.speak();
cat.speak();
turtle.speak();
}
}
This class shows three strong alternatives to conditionals:
- Replace complex
switch
logic with enums + abstract methods. - Use a map of commands to handle input-based logic.
- Apply polymorphism instead of big
if
chains to choose behavior based on object type.
The java project can be found here.
Comments
One response to “Escape the If Jungle”
The enum tip is great! Thanks a lot!