Command Pattern

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. 

UML Representation


The following lists the benefits of using the Command pattern:
  • It 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.

When to Use

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.
Subpages (1): Command Pattern Example