命令设计模式

时间:2011-01-01 02:17:57

标签: command design-patterns

阅读命令设计模式后,我有一个问题 -

为什么我们在客户端上创建具体的命令和接收器对象。这不能在invoker类初始化吗?

我认为客户端应该创建调用者并将其请求传递给调用者。祈求者应该照顾好所有的东西。

通过这样做,

  1. 我们对客户的依赖程度较低。
  2. 类图的设计与实际设计完全不同。
  3. 示例 - 客户创建一些买卖股票的订单。然后将订单发送给代理商。代理商接收订单并将其放入StockTrade系统。

    这里的代理商是调用者,股票交易系统是接收者。

    根据命令设计模式:

    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
        }
    }
    

    现在该类依赖于接收器,具体命令和代理。

    我们不能从客户端移动接收器和具体的命令依赖吗?

    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
        }
    }
    

1 个答案:

答案 0 :(得分:1)

它实际上归结为您拥有多少命令以及您希望从调用者公开多少接口。如果最终为每个命令公开一个接口,那么为什么要先创建命令对象呢。整个命令模式的想法是将调用者逻辑与命令数量分离,因为增加了Agent类不应该进行任何更改的命令数。