Command | Cheat Sheet

Behavioral Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
4 min readJul 12, 2022

--

Command is a behavioral design pattern which turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as method arguments, delay or queue a request’s execution, and support undoable operations. [https://refactoring.guru/design-patterns/command]

Real-life examples

  • Order in a restaurant: Waiter takes the order, which is the customer’s command — Waiter tells the cook, who then has all the information to cook the dish
  • In the ice cream parlour, the cook would be the ice cream maker — but taking the order remains the same
  • Postal office
  • Application controller: Handling of UI events like button clicks

Meaning

  • Encapsulation of a request as an object, thus enabling parameterisation of clients with different requests, queuing, or logging operations (+ undo)

Applicability (suitable if …)

  • Objects need to be parameterised with an executing operation (e.g. by callback in procedural languages)
  • A request is specified and then queued and executed at different times
  • UNDO (undoing of operations) needs to be supported (command history)
  • Logging of changes made needs to be supported (e.g. recovery after system crash)
  • A system with complex operations needs to be structured, based on primitive operations (e.g. transactions)

Assets and Drawbacks

  • Decoupling of an object that triggers the operation from another object that is able to perform it
  • Command objects are first-order objects that can be manipulated and extended like other objects
  • Several commands can be combined into a single command object

Example

As an example, the behavior of a text editor can be implemented. This opens, edits, saves, and closes different files.

The Receiver can perform the operations associated with the machining. Any class can serve as a Receiver. The Receiver of the example implements the actions of a text-editor:

Receiver

The Command declares an interface for the execution of an operation:

Command

The concrete classes of the Command define a binding between a Receiver and the operation. They also implement the execute() command:

Concrete Command

Finally, the Invoker needs to be implemented, which instructs the Command object to execute the request:

Invoker

The Client creates a concrete Command object and assigns its Receiver:

Client

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Behavioral Patterns deal with algorithms and assignment of responsibilities to objects. Furthermore, they describe mutual communication patterns and grasp complex program sequences. They are therefore very well suited for use in areas where complicated code needs to be made readable and maintainable. Extensions are, in consequence, very good and quickly implementable.

Follow me on Medium, or Twitter, or subscribe here on Medium to read more about DevOps, Agile & Development Principles, Angular and other useful stuff. Happy Coding! :)

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links