🌱Aadam's Garden

Search

Search IconIcon to open search

Command Pattern

Last updated Aug 16, 2022

Source:: Software Design Patterns - Best Practices for Software Developers


Definition

The pattern is defined as representing an action or a request as an object that can then be assed to other objects as parameters, allowing parameterization of clients with requests or actions. The requests can be queued for later execution or logged. Logging requests enables undo operations.

The Command pattern aims to encapsulate method invocation, requests or operations into a single object and gives you the ability to both parameterize and pass method calls around that can be executed at your discretion. In addition, it enables you to decouple objects invoking the action from the objects which implement them, giving you a greater degree of overall flexibility in swapping out concrete ‘classes’. 1

The main idea behind the command pattern is that it provides you a means to separate the responsibilities of issuing commands from anything executing commands, delegating this responsibility to different objects instead. 1

The command pattern is equivalent of a callback function in procedural languages as we parametrize objects with an action to perform. The command objects can also be queued for later execution.

# Resources