Command Pattern

command_implementation_-_uml_class_diagram

Definition of a Macro: “An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the basis of the Command design pattern.

The Command Pattern encapsulates commands (method calls) in objects, allowing us to issue requests without knowing the requested operation or the requesting object. This pattern provides the options to queue commands, undo/redo actions etc.

Overview

  • Encapsulate a request in an object
  • Allows the parameterization of clients with different requests
  • Allows saving the requests in a queue

The classes participating in the pattern are:

  • Command
    • declares an interface for executing an operation
  • ConcreteCommand
    • extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.
  • Client
    • creates a ConcreteCommand object and sets its receiver
  • Invoker
    • asks the command to carry out the request
  • Receiver
    • knows how to perform the operations

The Client asks for a command to be executed. The Invoker takes the command, encapsulates it and places it in a queue, in case there is something else to do first, and the ConcreteCommand that is in charge of the requested command, sending its result to the Receiver.

I’ve basically nicked all this, word for word, from the source below but it seemed pointless to reword and rework it too much. It’s good info.

 

TRP

source: www.oodesign.com/command-pattern.html
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s