Behavioural Design Pattern: Command Design pattern

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:01:00

Define Command Design Pattern

In this pattern, a request is wrapped as a command within an object and sent to the invoker. The invoker object routes the command to the relevant object that can handle it, and that object executes the command.

Examples of Command Design Patterns

Examples of command design patterns include:

  • Create menu systems for apps like editors and IDEs.
  • Update user interfaces with commands.
  • Auditing & logging all changes made via commands.

Command Pattern with UML diagram

The classes, interfaces, & objects in the above UML class diagram are defined as follows.

  • Client: This is the class that generates and executes command objects.
  • Invoker: Requests that the command does an action.
  • Command: This interface defines the Execute operation.
  • ConcreteCommand: This class executes operations on the receiver.
  • Receiver: This is the class that executes the Action connected with the request.

When should you use the Command Design Pattern?

The Command Design Pattern is applicable in the following cases:

  • Callback functionality needs to be implemented.
  • Commands must be supported with redo and undo functionality.
  • Sending queries to several receivers can result in different responses.
  • All command-based changes must be audited and logged.

Advantages of the Command Design Pattern

  • Separation of Concerns: Separates the object that initiates the activity from the entity that understands how to conduct it.
  • Extension: New commands can be introduced without modifying the existing code.
  • Composite Commands: You can combine many commands into one command.
  • Undo/Redo Operations: You can preserve a history of commands and use undo/redo functionality.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this