zoukankan      html  css  js  c++  java
  • 设计模式

    /// <summary>
    /// 烧烤的人
    /// </summary>
    class Barbecuer
    {
        public void Meat()
        {
            Console.WriteLine("bbq meat");
        }
    
        public void Vegetable()
        {
            Console.WriteLine("bbq vegetable");
        }
    }                                                        
    
    /// <summary>
    /// 烧烤命令抽象类
    /// </summary>
    abstract class Command
    {
        protected Barbecuer receiver;
    
        public Command(Barbecuer boy)
        {
            this.receiver = boy;
        }
    
        public abstract void ExcuteCommand();
    }
    
    /// <summary>
    /// 烤肉的命令
    /// </summary>
    class BarMeatCommand : Command
    {
        public BarMeatCommand(Barbecuer boy) : base(boy) { }
    
        public override void ExcuteCommand()
        {
            receiver.Meat();
        }
    }
    
    /// <summary>
    /// 烤菜的命令
    /// </summary>
    class BarVegetableCommand : Command
    {
        public BarVegetableCommand(Barbecuer boy) : base(boy) { }
    
        public override void ExcuteCommand()
        {
            receiver.Vegetable();
        }
    }
    
    class Waiter
    {
        IList<Command> _command = new List<Command>();
    
        public void SetOrder(Command command)
        {
            this._command.Add(command);
            Console.WriteLine("增加订单{0},时间{1}", command.ToString(), DateTime.Now.ToString());
        }
    
        public void CancelOrder(Command command)
        {
            this._command.Remove(command);
            Console.WriteLine("删除订单{0},时间{1}", command.ToString(), DateTime.Now.ToString());
        }
    
        public void Notify()
        {
            foreach (Command command in _command)
            {
                command.ExcuteCommand();
            }
        }
    }
    
    class CommandModelViewModel
    {
        public CommandModelViewModel()
        {
            // 烧烤师傅
            Barbecuer boy = new Barbecuer();
    
            // 服务员
            Waiter waiter = new Waiter();
    
            Command meatCommand1 = new BarMeatCommand(boy);
            Command meatCommand2 = new BarMeatCommand(boy);
            Command vegetableCommand = new BarVegetableCommand(boy);
    
            waiter.SetOrder(meatCommand1);
            waiter.SetOrder(meatCommand2);
            waiter.SetOrder(vegetableCommand);
    
            waiter.Notify();
        }
    }
    

    一开始,用紧耦合的方式,就是客户端直接去调用执行命令的接受者,这样会导致:
    1)命令的是否能被执行失去控制;
    2)命令的执行未能被记录;
    3)命令的执行未能被撤销、重做。

    而使用命令模式:
    执行者有各个方法;
    抽象命令类指定执行执行者、抽象执行方法;
    某个命令类的执行方法为调用执行者的某个方法;
    由控制类维护一个命令队列,并控制命令的执行、记录。

  • 相关阅读:
    leetcode 78. 子集 JAVA
    leetcode 91. 解码方法 JAVA
    leetcode 75. 颜色分类 JAVA
    leetcode 74 搜索二维矩阵 java
    leetcode 84. 柱状图中最大的矩形 JAVA
    last occurance
    first occurance
    classical binary search
    LC.234.Palindrome Linked List
    LC.142. Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14199148.html
Copyright © 2011-2022 走看看