CSC/ECE 517 Fall 2010/ch4 4f sv: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
= '''Command Pattern in static and dynamic languages''' = | |||
== What is Command Pattern ? == | == What is Command Pattern ? == |
Revision as of 01:09, 16 October 2010
Command Pattern in static and dynamic languages
What is Command Pattern ?
The command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters. It encapsulates a request as an object and gives it a known public interface. It ensures that every object receives its own commands and provides a decoupling between sender and receiver. In this case a sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.
Why and When do we use a Command Pattern?
Command Pattern comes into picture when there is a need to store objects behaviour as a command and its state. The Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.
There are several benefits of using the Command pattern:
> It provides encapsulation of application logic so that it can be executed at a different point of time.
> It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.
> The separation between application logic and context allows to easier exchange the application logic.
Components of a command pattern
<< to add diagram >>
From the diagram above the major components for the command pattern can be listed out as follows.
Client: Invokes a particular module using a command and passes a request which gets propagated as a command.
Command: It is an object that encapsulates a request to the receiver. Command request maps to particular modules. According to the command, a module is invoked.
Receiver: It is the component that is acted upon by each request. It knows how to perform the operations associated with carrying out a certain request.
Invoker: It is the class that is invoked by the client. It takes in the request and calls the receiver by passing the command to it and asks it to carry out the request.
Concrete Command: <<to add matter>>