Escape the If Jungle

Conditional Control

Modern Conditional Strategies in Java

Classic Conditionals:

  1. if
  2. if else
  3. if - else if - else
  4. switch
  5. Ternary operator (condition ? a : b)

Advanced or Alternative Structures:

  1. Map<K, Runnable> or Map<K, Function> – to simulate switch/if chains for behavior
  2. enum with behavior (Strategy Pattern) – enums can define a method differently for each constant
  3. Polymorphism – define different implementations for an interface instead of conditional logic
  4. Command Pattern – store commands in a map or list and execute dynamically
  5. Chain of Responsibility Pattern – each handler decides whether to handle or pass along
  6. Reflection – dynamically invoke methods (not recommended for everyday logic, but possible)
  7. 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”

  1. carlos Avatar
    carlos

    The enum tip is great! Thanks a lot!