title: Command Design Pattern
body: <p>After reading command design pattern, I have a question -</p>
  
  <p>Why we are creating concrete command and receiver object on client. Can't this initializatied on invoker class? </p>
  
  <p>I think client should create invoker and pass it's request to invoker. Invoker should take care of all the stuff.</p>
  
  <p>By doing this,</p>
  
  <ol>
  <li>We have less dependency on client.</li>
  <li>The design of class diagram is totally different from actual design. </li>
  </ol>
  
  <p>example - The client creates some orders for buying and selling stocks. Then the orders are sent to the agent.The agent takes the orders and place them to the StockTrade system.</p>
  
  <p>Here agent is invoker and stock trade system is receiver.</p>
  
  <p>as per command design pattern:</p>
  
  <pre><code>public interface IOrder 
  {
  public abstract void execute ( );
  }
  
  class StockTrade {
      public void buy() {
          System.out.println("You want to buy stocks");
      }
      public void sell() {
          System.out.println("You want to sell stocks ");
      }
  }
  
  class Agent {
      public Agent() 
      {
      }
  
      void placeOrder(IOrder order) {
          order.execute();
      }    
  }
  
  //ConcreteCommand Class.
  class BuyStockOrder implements Order {
      private StockTrade stock;
      public BuyStockOrder ( StockTrade st) {
          stock = st;
      }
      public void execute( ) {
          stock . buy( );
      }
  }
  
  //ConcreteCommand Class.
  class SellStockOrder implements IOrder { 
      private StockTrade stock;
      public SellStockOrder ( StockTrade st) {
          stock = st;
      }
      public void execute( ) {
          stock . sell( );
      }
  }
  
  // Client
  public class Client {
      public static void main(String[] args) {
          StockTrade stock = new StockTrade();
          BuyStockOrder bsc = new BuyStockOrder (stock);
          SellStockOrder ssc = new SellStockOrder (stock);
          Agent agent = new Agent();
  
          agent.placeOrder(bsc); // Buy Shares
          agent.placeOrder(ssc); // Sell Shares
      }
  }
  </code></pre>
  
  <p>Now the class has dependency on receiver, concrete command and agent.</p>
  
  <p>Can't we move the receiver and concrete command dependency from client?</p>
  
  <pre><code>Agent Class
  class Agent {
      public Agent() 
      {
      }
  
      void BuyStock(BuyStockRequest request) 
      {
          IOrder order = new BuyStockOrder();
      order.execute(request);
      } 
  
      void SellStock(SellStockRequest request) 
      {
          IOrder order = new SellStockOrder();
      order.execute(request);
      }   
  }
  
  //ConcreteCommand Class.
  class BuyStockOrder implements IOrder {
      private StockTrade stock;
      public BuyStockOrder () {
          stock = new StockTrade;
      }
      public void execute(BuyStockRequest request) {
          stock.Name = request.Name;
      stock.Price = request.Price;
          stock.buy( );
      }
  }
  
  //ConcreteCommand Class.
  class SellStockOrder implements IOrder {
      private StockTrade stock;
      public SellStockOrder () {
          stock = new StockTrade;
      }
      public void execute(SellStockRequest request) {
          stock.Name = request.Name;
      stock.Price = request.Price;
          stock.sell( );
      }
  }
  
  
  //Request Class
  class BuyStockRequest
  {
    public string Name {get; set;}
    public decimal price {get; set;}
  }
  
  class SellStockRequest
  {
    public string Name {get; set;}
    public decimal price {get; set;}
  }
  
  Client
  public class Client {
      public static void main(String[] args) {
          BuyStockRequest request = new BuyStockRequest();
          request.Name = "XYZ";
      request.Price = 100;
          Agent agent = new Agent();
          agent.BuyStock(request); // Buy Shares
      }
  }
  </code></pre>