Open In App

Command Design Pattern

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Command Design Pattern is a behavioral design pattern that turns a request into a stand-alone object, allowing parameterization of clients with different requests, queuing of requests, and support for undoable operations(action or a series of actions that can be reversed or undone in a system).

command-design-pattern-new-

What is the Command Design Pattern?

The Command Design Pattern is a behavioral design pattern that turns a request into a stand-alone object, allowing parameterization of clients with different requests, queuing of requests, and support for undoable operations.

  • The Command Pattern encapsulates a request as an object, allowing for the separation of sender and receiver.
  • Commands can be parameterized, meaning you can create different commands with different parameters without changing the invoker(responsible for initiating command execution).
  • It decouples the sender (client or invoker) from the receiver (object performing the operation), providing flexibility and extensibility.
  • The pattern supports undoable(action or a series of actions that can be reversed or undone in a system) operations by storing the state or reverse commands.

Components of the Command Design Pattern

1. Command Interface

The Command Interface is like a rulebook that all command classes follow. It declares a common method, execute(), ensuring that every concrete command knows how to perform its specific action. It sets the standard for all commands, making it easier for the remote control to manage and execute diverse operations without needing to know the details of each command.

2. Concrete Command Classes

Concrete Command Classes are the specific commands, like turning on a TV or adjusting the stereo volume. Each class encapsulates the details of a particular action. These classes act as executable instructions that the remote control can trigger without worrying about the nitty-gritty details of how each command accomplishes its task.

3. Invoker (Remote Control)

The Invoker, often a remote control, is the one responsible for initiating command execution. It holds a reference to a command but doesn’t delve into the specifics of how each command works. It’s like a button that, when pressed, makes things happen. The remote control’s role is to coordinate and execute commands without getting involved in the complexities of individual actions.

4. Receiver (Devices)

The Receiver is the device that knows how to perform the actual operation associated with a command. It could be a TV, stereo, or any other device. Receivers understand the specific tasks mentioned in commands. If a command says, “turn on,” the Receiver (device) knows precisely how to execute that action. The Receiver-Command relationship separates responsibilities, making it easy to add new devices or commands without messing with existing functionality.

Command Design Pattern example

Imagine you are tasked with designing a remote control system for various electronic devices in a smart home. The devices include a TV, a stereo, and potentially other appliances. The goal is to create a flexible remote control that can handle different types of commands for each device, such as turning devices on/off, adjusting settings, or changing channels.

What can be the challenges while implementing this system?

  • First challenges is that devices can have different functionalities, so designing a remote control that can seamlessly handle different device types with varying functionalities without becoming overly complex or device-specific.
  • Implementing a remote control that supports various commands without tightly coupling ensuring the remote control can execute commands for different devices without needing extensive modifications for each new command or device type.
  • Designing a system that allows users to customize the behavior of the remote control dynamically

How Command Pattern help to solve above challenges?

The Command Pattern can be employed to address these challenges. It introduces a level of abstraction between the sender of a command (remote control) and the receiver of the command (electronic devices).

  • The Command Pattern decouples the sender (Invoker) from the receiver (Devices). The remote control doesn’t need to know the specific details of how each device operates; it only triggers commands.
  • New devices or commands can be added without modifying existing code. The remote control can work with any device that implements the common command interface.
  • The remote control can dynamically change its behavior by associating different commands.

CommandPatternExampledrawio-(3)

Below is the code of above problem statement using Command Pattern:

1. Command Interface

The Command interface declares a method, often named execute(). This method is meant to encapsulate a specific operation. The interface sets a contract for concrete command classes, defining the execute() method that encapsulates the operation to be performed.

Java




// Command interface
public interface Command {
    void execute();
}