Let's first make sure we know what the Command pattern is:
Command pattern encapsulates a request
as an object and gives it a known
public interface. Command Pattern
ensures that every object receives its
own commands and provides a decoupling
between sender and receiver. A sender
is an object that invokes an
operation, and a receiver is an object
that receives the request and acts on
it.
Here's an example for you. There are many ways you can do this, but I am going to take an interface base approach to make the code more testable for you. I am not sure what language you prefer, but I am writing this in C#.
First, create an interface that describes a Command.
public interface ICommand
{
void Execute();
}
Second, create command objects that will implement the command interface.
public class CutCommand : ICommand
{
public void Execute()
{
// Put code you like to execute when the CutCommand.Execute method is called.
}
}
Third, we need to setup our invoker or sender object.
public class TextOperations
{
public void Invoke(ICommand command)
{
command.Execute();
}
}
Fourth, create the client object that will use the invoker/sender object.
public class Client
{
static void Main()
{
TextOperations textOperations = new TextOperations();
textOperation.Invoke(new CutCommand());
}
}
I hope you can take this example and put it into use for the application you are working on. If you would like more clarification, just let me know.