Monday, 27 July 2015

The Command Pattern

The Command Pattern


Defined

 The Command Pattern encapsulates a request as an object, thereby letting you parametrize other objects with different requests, ques or log request and support undo-able operations.

- A command object encapsulates a request, by binding together a set of actions on a specific receiver. To achieve this, it packages the actions and the receiver up into an object that exposes just one method, execute(). When called, execute() causes the actions to be invoked on the receiver,From the outside, no other objects really know what actions get performed on what receiver, they just know that if they call the execute method their request will be serviced.


Diagrams




Code Examples







Macro Commands
- A Kind of command that can execute other commands... and more then one of them.


Bullet Points

  • The Command Pattern decouples an object, making a request from the one that knows how to perform it.
  • A Command object is at the center of this decoupling and encapsulates a receiver with an action (or set of actions).
  • An invoker makes a request of a Command object by calling its execute() method, which invokes those actions on the receiver.
  •  Invokers can be parametrized with Commands, even dynamically at run-time.
  • Commands may support undo by implementing an undo method that restores the object to its previous state before the execute() method was last called.
  • Macro Commands are a simple extension of Command that allow multiple commands to be invoked. likewise Macro Commands can easily support undo().
  • In practice, it is not uncommon for "smart" Command Objects to implement the request themselves rather than delegating to a receiver.
  • Commands may also be used to implement logging and transaction systems.

No comments:

Post a Comment