title: Why should I use the command design pattern while I can easily call required methods?
body: <p>I am studying the <a href="https://en.wikipedia.org/wiki/Command_pattern">command design pattern</a>, and I am quite confused with the way of using it. The example that I have is related to a remote control class that is used to turn lights on and off.</p>
  
  <p>Why should I not use the switchOn() / switchOff() methods of Light class rather than having separate classes and methods that eventually call switchOn / switchOff methods?</p>
  
  <p><strong><em>I know my example is quite simple</em>, but that is the point. I could not find any complex problem anywhere on the Internet to see the exact usage of the command design pattern.</strong></p>
  
  <p><strong>If you are aware of any complex real world problem that you solved that can be solved using this design pattern please share that with me. It helps me and future readers of this post to better understand the usage of this design pattern. Thanks</strong></p>
  
  <pre><code>//Command
  public interface Command {
    public void execute();
  }
  
  //Concrete Command
  public class LightOnCommand implements Command {
  
    //Reference to the light
    Light light;
  
    public LightOnCommand(Light light) {
      this.light = light;
    }
  
    public void execute() {
      light.switchOn();        //Explicit call of selected class's method
    }
  }
  
  //Concrete Command
  public class LightOffCommand implements Command {
  
    //Reference to the light
    Light light;
  
    public LightOffCommand(Light light) {
      this.light = light;
    }
  
    public void execute() {
      light.switchOff();
    }
  }
  
  //Receiver
  public class Light {
    private boolean on;
  
    public void switchOn() {
      on = true;
    }
  
    public void switchOff() {
      on = false;
    }
  }
  
  //Invoker
  public class RemoteControl {
    private Command command;
  
    public void setCommand(Command command) {
      this.command = command;
    }
  
    public void pressButton() {
      command.execute();
    }
  }
  
  //Client
  public class Client {
    public static void main(String[] args) {
      RemoteControl control = new RemoteControl();
      Light light = new Light();
      Command lightsOn = new LightsOnCommand(light);
      Command lightsOff = new LightsOffCommand(light);
  
      //Switch on
      control.setCommand(lightsOn);
      control.pressButton();
  
      //Switch off
      control.setCommand(lightsOff);
      control.pressButton();
    }
  }
  </code></pre>
  
  <p><strong>Why should not I easily use code like the following?</strong></p>
  
  <pre><code> Light light = new Light();
   switch(light.command) {
    case 1:
      light.switchOn();
      break;
    case 2:
      light.switchOff();
      break;
   }
  </code></pre>