Unleashing the Power of Command Design Pattern in Your Code

Akshay Aryan
Java Developers Community
3 min readJan 2, 2024

--

Have you ever thought about your code as a remote control for your devices? The Command Design Pattern lets you do just that in the world of programming. In this post, we’ll explore the simplicity and effectiveness of the Command Pattern.

What is the Command Design Pattern ? Imagine a TV remote control. When you press a button, it sends a command to the TV. The Command Design Pattern works similarly in code. It turns requests into objects, allowing you to change and queue commands without altering the code that uses them.

Components of the Command Pattern:

  1. Command Interface/Abstract Class: Declares an execute method.
  2. ConcreteCommand: Implements the Command interface, linking a specific action to the object responsible for it.
  3. Client: Initiates requests by creating command objects.
  4. Invoker: Requests the command to execute the operation.
  5. Receiver: Knows how to perform the operation associated with the command.

Example: Controlling a Light Switch Let’s think of a light switch. Instead of directly turning the light on or off, we create a command object representing these actions. The client (you with a remote) doesn’t need to know how the light works; it just knows how to press the button (execute the command).

Step 1: We create a Command interface with a single method execute(). This interface represents the common interface for all commands.

// Step 1: Command interface
interface Command {
void execute();
}

Step 2: We create a LightOnCommand class implementing the Command interface. This class encapsulates the action of turning on a light. It has a reference to a Light object and, when executed, calls the turnOn() method on that light.

// Step 2: ConcreteCommand
class LightOnCommand implements Command {
private Light light;

public LightOnCommand(Light light) {
this.light = light;
}

public void execute() {
light.turnOn();
}
}

Step 3: We have a Light class, which is the receiver. It knows how to perform the operations associated with turning the light on or off.

// Step 3: Receiver
class Light {
public void turnOn() {
System.out.println("Light is ON");
}

public void turnOff() {
System.out.println("Light is OFF");

Step 4: The RemoteControl class is the invoker. It has a Command and can set a command using the setCommand method. When pressButton is called, it executes the command.

// Step 4: Invoker
class RemoteControl {
private Command command;

public void setCommand(Command command) {
this.command = command;
}

public void pressButton() {
command.execute();
}
}

Step 5: In the main method of the Client class, we create a Light object and a corresponding LightOnCommand. We then create a RemoteControl, set the command to turn on the light, and press the button. As a result, the light is turned on, and the message "Light is ON" is printed to the console.

// Step 5: Client
public class Client {
public static void main(String[] args) {
// Create a Light and a corresponding command
Light light = new Light();
Command lightOnCommand = new LightOnCommand(light);

// Create a remote control and set the command
RemoteControl remote = new RemoteControl();
remote.setCommand(lightOnCommand);

// Press the button on the remote control
remote.pressButton(); // Outputs: Light is ON
}
}

Benefits of the Command Pattern:

  1. Decoupling: Commands separate the sender (client) from the receiver (object that performs the action).
  2. Flexibility: Easily switch and queue commands without changing existing code.
  3. Undo/Redo Operations: Ideal for implementing undoable operations.

Conclusion: The Command Design Pattern is a powerful tool, simplifying code and making it more flexible. Just like a remote control brings convenience to managing devices, the Command Pattern brings elegance to managing actions in your code. Try incorporating it into your projects, and watch your code become more organized and maintainable.

Thank you for being a part of our community!

Having journeyed this far, your clap, comments, and a follow would be the perfect companions to our shared adventure. Let’s continue this exciting ride together! 👏

command design pattern

--

--

Akshay Aryan
Java Developers Community

Cultivating a passion for technology and delving into intriguing, lesser-known tech facts.