Search code examples
javadesign-patternscommand-pattern

Long list of if statements in Java


Sorry I can't find a question answering this, I'm almost certain someone else has raised it before.

My problem is that I'm writing some system libraries to run embedded devices. I have commands which can be sent to these devices over radio broadcasts. This can only be done by text. inside the system libraries I have a thread which handles the commands which looks like this

if (value.equals("A")) { doCommandA() }
else if (value.equals("B")) { doCommandB() } 
else if etc. 

The problem is that there are a lot of commands to it will quickly spiral to something out of control. Horrible to look out, painful to debug and mind boggling to understand in a few months time.


Solution

  • using Command pattern:

    public interface Command {
         void exec();
    }
    
    public class CommandA() implements Command {
    
         void exec() {
              // ... 
         }
    }
    
    // etc etc
    

    then build a Map<String,Command> object and populate it with Command instances:

    commandMap.put("A", new CommandA());
    commandMap.put("B", new CommandB());
    

    then you can replace your if/else if chain with:

    commandMap.get(value).exec();
    

    EDIT

    you can also add special commands such as UnknownCommand or NullCommand, but you need a CommandMap that handles these corner cases in order to minimize client's checks.