Hello World


Tuesday, December 11, 2007

Command Design Pattern

Encapsulate a request as an object.
What does that mean?

Example
The code demonstrates the Command pattern, which stores requests as objects allowing clients to execute or playback the requests.
All Commands implement the same interface, so they can be handled polymorphically. Typically their interface includes methods such as Do and Undo (or Execute and Undo).
In this case we invoke a method called Execute that could perform any number of tasks.

There are three players in the Command Design Pattern.

The Receiver: As the name suggests is the eventual recipient of the command.
The Command: The Object that encapsulates the request.
The Invoker: You could say the broker of the command. It’s the Invokers job to make sure the command is stored and processed when required.

The Invoker does not store requests it stores Command Objects that encapsulate requests.

Below in the example is the Receiver Object. I has a method named Action. The Action method call in the Receiver object is Vendor specific. It could be any method call that needs to be invoked, i.e.: DataBase.Update(....) etc..

Also we have the Command class, it can have any number of methods that perform a variety of functions. By adding this layer of Abstraction we don't tie our Receivers to the actual true function calls. We keep everything de-coupled.
This allows for new commands to be added by implementing the abstract Command class and setting the invoker.SetCommand(command (Reference to Abstract Command)) with it.

In the following sample we Create a Command object, pass in to the constructor the receiver (Reference) of the command and then store all that in the Invoker.
When we need the the Receiver.Action() method to be invoked we do it via the invoker.ExecuteCommand().

The invoker.ExecuteCommand polymorphically calls Command.Execute which calls the true Receiver.Action function.

namespace Martins.Command.Pattern
{
//MainApp startup
//Command Design Pattern.
class MainApp
{
//Entry point into console application.
static void Main()
{
//Create receiver, command, and invoker
Receiver receiver = new Receiver();
Command command = new ConcreteCommand(receiver);
Invoker invoker = new Invoker();

//Set and execute command
invoker.SetCommand(command);
invoker.ExecuteCommand();
}
}
//Command
abstract class Command
{
protected Receiver receiver;

//Constructor
public Command(Receiver receiver)
{
this.receiver = receiver;
}
public abstract void Execute();
}
//ConcreteCommand
class ConcreteCommand : Command
{
//Constructor
public ConcreteCommand(Receiver receiver) :
base(receiver)
{
}
public override void Execute()
{
receiver.Action();
}
}
//Receiver
class Receiver
{
public void Action()
{
Console.WriteLine("Called Receiver.Action()");
}
}
//Invoker
class Invoker
{
private Command command;
public void SetCommand(Command command)
{
this.command = command;
}
public void ExecuteCommand()
{
command.Execute();
}
}
}

No comments:

4GuysFromRolla.com Headlines