Course Content
Low Level System Design
LLD Topics
High Level System Design
Low & High Level System Design
Command 

The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object containing all the information needed to perform the action later. It helps encapsulate a command as an object, thereby decoupling the sender of the request from the receiver that performs the action.



Why Use the Command Pattern?

Imagine a system where one component wants something to be done (like a user clicking a button), but another component is responsible for actually performing the action. If the sender and receiver are tightly coupled, any changes to the receiver might break the sender.

 

The Command Pattern solves this by separating the sender (invoker) from the receiver (the actual executor of the command). It allows you to:

 

  • Parameterize requests
  • Queue commands
  • Undo/redo actions
  • Log requests for auditing


Real-World Analogy

Think of a restaurant:

 

  • The waiter takes your order (this is the command).
  • The chef cooks the food (this is the receiver).
  • The waiter doesn’t care how the chef prepares the dish; he simply passes along the order.

 

This separation of roles allows for flexibility and independence—waiters and chefs can be replaced or changed without affecting each other.



Key Components

  • Command – Declares an interface for executing operations.
  • ConcreteCommand – Implements the command and defines a binding between a receiver and an action.
  • Receiver – Knows how to perform the operations associated with carrying out a request.
  • Invoker – Asks the command to carry out the request.
  • Client – Creates the command object and sets its receiver.


Common Use Cases

  • Undo/Redo systems – Commands can be stored and reversed.
  • Macro recording – Sequence of commands can be replayed later.
  • GUI buttons and menu actions – Encapsulate user actions into command objects.
  • Task queues – Commands can be added to a queue and executed later.
  • Remote control systems – Different buttons can trigger different command objects.


Advantages of the Command Pattern

  • Decouples sender and receiver – Easier to change or replace either part.
  • Supports undo/redo – Because commands can store previous state.
  • Facilitates logging – Every command can be recorded for auditing or replay.
  • Flexible and extensible – Easy to add new commands without modifying existing code.


Limitations

  • Increased complexity – Many classes may be created for each command.
  • Overhead – May seem heavy for simple operations.

 

Summary

The Command Pattern is essential when you want to delegate requests in a way that keeps your system flexible and decoupled. It provides a way to store, delay, and log requests, making it ideal for systems that need undo functionality, task queues, or reusable operations.

0% Complete
WhatsApp Icon

Hi Instagram Fam!
Get a FREE Cheat Sheet on System Design.

Hi LinkedIn Fam!
Get a FREE Cheat Sheet on System Design

Loved Our YouTube Videos? Get a FREE Cheat Sheet on System Design.