Command:
Encapsulates a request as an object, thereby letting you parametrize
clients with
different requests, queue or log requests, and support
undoable operations.
A Command Pattern allows requests to an object to exist as an object. What does that mean? It means that if you send a request for some function to an object, the command object can house that request inside the object. This is useful in the case of undoing or redoing some action, or simply storing an action in a request queue on an object. When you send the request, it is stored in the object. Then later, if you need to access that same request, or apply the request or some method on the request to an object, you can use the request object instead of calling the object's method directly.
The Command pattern has three main components: the Invoker, the Command, and the Receiver. The invoker component acts as a link between the commands and the receiver, and houses the receiver and the individual commands as they are sent. The command is an object that encapsulates a request to the receiver. The receiver is the component that is acted upon by each request.
**** Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
* The classes and/or objects participating in this pattern are:
Command (Command)
***declares an interface for executing an operation
* ConcreteCommand (CalculatorCommand): defines a binding between a Receiver object and an action
implements Execute by invoking the corresponding operation(s) on Receiver
Client (CommandApp): creates a ConcreteCommand object and sets its receiver
Invoker (User): asks the command to carry out the request
Receiver (Calculator): knows how to perform the operations associated with carrying out the request.
class Program
{
static void Main(string[] args)
{
Receiver receiver = new Receiver();
Command command = new ConcreteCommand(receiver);
Invoker invoker = new Invoker();
//Set and execute command
invoker.SetCommand(command);
invoker.ExecuteCommand();
Console.ReadKey();
}
}
//abstract class
abstract class Command
{
protected Receiver receiver;
public Command(Receiver receiver)
{
this.receiver = receiver;
}
public abstract void Execute();
}
//Concrete command class
class ConcreteCommand : Command
{
public ConcreteCommand(Receiver receiver) : base(receiver) { }
public override void Execute()
{
receiver.Action();
}
}
//Receiver Class
class Receiver
{
public void Action()
{
Console.WriteLine("Called Receiver.Action");
}
}
//Invoker Class
class Invoker
{
private Command _command;
public void SetCommand(Command command)
{
this._command = command;
}
public void ExecuteCommand()
{
_command.Execute();
}
}
A Command Pattern allows requests to an object to exist as an object. What does that mean? It means that if you send a request for some function to an object, the command object can house that request inside the object. This is useful in the case of undoing or redoing some action, or simply storing an action in a request queue on an object. When you send the request, it is stored in the object. Then later, if you need to access that same request, or apply the request or some method on the request to an object, you can use the request object instead of calling the object's method directly.
The Command pattern has three main components: the Invoker, the Command, and the Receiver. The invoker component acts as a link between the commands and the receiver, and houses the receiver and the individual commands as they are sent. The command is an object that encapsulates a request to the receiver. The receiver is the component that is acted upon by each request.
**** Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
* The classes and/or objects participating in this pattern are:
Command (Command)
***declares an interface for executing an operation
* ConcreteCommand (CalculatorCommand): defines a binding between a Receiver object and an action
implements Execute by invoking the corresponding operation(s) on Receiver
Client (CommandApp): creates a ConcreteCommand object and sets its receiver
Invoker (User): asks the command to carry out the request
Receiver (Calculator): knows how to perform the operations associated with carrying out the request.
class Program
{
static void Main(string[] args)
{
Receiver receiver = new Receiver();
Command command = new ConcreteCommand(receiver);
Invoker invoker = new Invoker();
//Set and execute command
invoker.SetCommand(command);
invoker.ExecuteCommand();
Console.ReadKey();
}
}
//abstract class
abstract class Command
{
protected Receiver receiver;
public Command(Receiver receiver)
{
this.receiver = receiver;
}
public abstract void Execute();
}
//Concrete command class
class ConcreteCommand : Command
{
public ConcreteCommand(Receiver receiver) : base(receiver) { }
public override void Execute()
{
receiver.Action();
}
}
//Receiver Class
class Receiver
{
public void Action()
{
Console.WriteLine("Called Receiver.Action");
}
}
//Invoker Class
class Invoker
{
private Command _command;
public void SetCommand(Command command)
{
this._command = command;
}
public void ExecuteCommand()
{
_command.Execute();
}
}
No comments:
Post a Comment