- The command pattern allows encapsulation of the requests or operations into separate objects. It decouples the objects that send requests from the objects responsible for executing those requests.
- Consider an example where the client is accessing the methods of an API directly throughout the application. What will happen if the implementation of that API changes? The change will have to be made everywhere the API is being used. To avoid this, we could make use of abstraction and separate the objects requesting from those implementing the request. Now, if a change occurs, only the object making the call will need to change.
Programmatic Example
First of all we have the receiver that has the implementation of every action that could be performed
then we have an interface that each of the commands are going to implement and then we have a set of commands
Then we have an Invoker
with whom the client will interact to process any commands
Finally let’s see how we can use it in our client
Command pattern can also be used to implement a transaction based system. Where you keep maintaining the history of commands as soon as you execute them. If the final command is successfully executed, all good otherwise just iterate through the history and keep executing the undo
on all the executed commands.
- when to use command pattern
- queue and execute requests at different times
- perform operations such as reset or undo
- keep a history of requests made