Command Pattern
 
 
یکی از الگوهای دسته ی behavorial است که به کمک آن (هدف) :
میتوان یک عملیات را در قالب یک آبجکت Encapsulate کرد؛ لذا از آن میتوان در اموری مانند :
پارامتری کردن کلاینتها با درخواستهای متفاوت،
ثبت و log کردن درخواستها،
عملیات برگشت پذیر (Undoable)
و ... استفاده کرد.
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. 
 
یکی از کاربردهای دنیای واقعی برای این الگو گزینه های Undo و Redo موجود در تمامی محیطهای ویرایشگر میباشد.
از دیگر مثالهای واقعی این الگو میتوان به مورد ذخیره ی ماکروها (یکسری عملیات متوالی) در مجموعه ی آفیس اشاره کرد (همانطور که میدانید کاربر میتواند یکسری از کارهای پشت سر هم و تکراری خود را در قالب یک ماکرو ذخیره نماید و سپس با اجرای ماکرو تمامی آن کارها پشت سر هم اجرا خواهند شد)
یک مثال از سایت DoFactory را در زیر مشاهده میکنید
در این مثال ما یک ماشین حساب با تعداد نامتناهی عملیات Undo و Redo  خواهیم داشت :
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Command.RealWorld
{
  // MainApp test application
 
  class MainApp
  {
    static void Main()
    {
      // Create user and let her compute
       User user = new User();
      user.Compute('+', 100);
      user.Compute('-', 50);
      user.Compute('*', 10);
      user.Compute('/', 2);
      // Undo 4 commands
       user.Undo(4);
      // Redo 3 commands
       user.Redo(3);
      // Wait for user
       Console.Read();
    }
  }
  // "Command"
 
  abstract class Command
  {
    public abstract void Execute();
    public abstract void UnExecute();
  }
  // "ConcreteCommand"
 
  class CalculatorCommand : Command
  {
    char @operator;
    int  operand;
    Calculator calculator;
    // Constructor
     public CalculatorCommand(Calculator calculator,
      char @operator, int operand)
    {
      this.calculator = calculator;
      this.@operator  = @operator;
      this.operand    = operand;
    }
    public char Operator
    {
      set{ @operator = value; }
    }
    public int Operand
    {
      set{ operand = value; }
    }
    public override void Execute()
    {
      calculator.Operation(@operator, operand);
    }
  
    public override void UnExecute()
    {
      calculator.Operation(Undo(@operator), operand);
    }
    // Private helper function
     private char Undo(char @operator)
    {
      char undo;
      switch(@operator)
      {
        case '+': undo = '-'; break;
        case '-': undo = '+'; break;
        case '*': undo = '/'; break;
        case '/': undo = '*'; break;
        default : undo = ' '; break;
      }
      return undo;
    }
  }
  // "Receiver"
 
  class Calculator
  {
    private int curr = 0;
    public void Operation(char @operator, int operand)
    {
      switch(@operator)
      {
        case '+': curr += operand; break;
        case '-': curr -= operand; break;
        case '*': curr *= operand; break;
        case '/': curr /= operand; break;
      }
      Console.WriteLine(
        "Current value = {0,3} (following {1} {2})",
        curr, @operator, operand);
    }
  }
  // "Invoker"
 
  class User
  {
    // Initializers
     private Calculator calculator = new Calculator();
    private ArrayList commands = new ArrayList();
    private int current = 0;
    public void Redo(int levels)
    {
      Console.WriteLine("\n---- Redo {0} levels ", levels);
      // Perform redo operations
       for (int i = 0; i < levels; i++)
      {
        if (current < commands.Count - 1)
        {
          Command command = commands[current++] as Command;
          command.Execute();
        }
      }
    }
    public void Undo(int levels)
    {
      Console.WriteLine("\n---- Undo {0} levels ", levels);
      // Perform undo operations
       for (int i = 0; i < levels; i++)
      {
        if (current > 0)
        {
          Command command = commands[--current] as Command;
          command.UnExecute();
        }
      }
    }
    public void Compute(char @operator, int operand)
    {
      // Create command operation and execute it
       Command command = new CalculatorCommand(
        calculator, @operator, operand);
      command.Execute();
      // Add command to undo list
       commands.Add(command);
      current++;
    }
  }
}
خروجی برنامه :
	
		
			
			
				
Current value = 100 (following + 100)
Current value =  50 (following - 50)
Current value = 500 (following * 10)
Current value = 250 (following / 2)
---- Undo 4 levels
Current value = 500 (following * 2)
Current value =  50 (following / 10)
Current value = 100 (following + 50)
Current value =   0 (following - 100)
---- Redo 3 levels
Current value = 100 (following + 100)
Current value =  50 (following - 50)
Current value = 500 (following * 10)
			
		 
	 
 این الگو سر فرصت کاملتر خواهد شد...
منابع :
http://en.wikipedia.org/wiki/Command_pattern
http://www.dofactory.com/Patterns/PatternCommand.aspx
http://www.vico.org/pages/PatronsDis...ern%20Command/
http://www.codeproject.com/KB/books/DesignPatterns.aspx