The Command pattern encapsulates a request in an object, which enables you to store the command, pass the command to a method, and return the command like any other object.
The figure below illustrates the Command Pattern.
The following lists the benefits of using the Command Pattern:
- Separates the object that invokes the operation from the one that knows how to perform it.
- It's easy to add new commands, because you don't have to change existing classes.
You should use the Command pattern when:
- You want to parameterize objects by an action to perform.
- You specify, queue, and execute requests at different times.
- You must support undo, logging, or transactions