The command pattern
The concept behind this pattern is to transform a request into an object in order to facilitate some actions, such as undo/redo, insertion into a queue, or tracking of the request.
Roles
The command pattern creates distance between the client that requests an operation and the object that can perform it. The request is encapsulated into an object. This object contains a reference to the receiver who will effectively execute the operation.
The real operation is managed by the receiver and the command is like an order; it only contains a reference to the invoker, the object that will perform the action and an execute function will call the real operation on the worker.
This pattern allows the following features:
Sending requests to different receivers
Queuing, logging, and rejecting requests
Undoable actions (the
execute
method can memorize the state and allows you to go back to that state)Encapsulate a request in an object
Allows the parameterization of clients with different...