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)命令的执行未能被撤销、重做。

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

  • 相关阅读:
    websocket+nodejs+redis实现消息订阅和发布系统
    nodejs下载图片到本地,根据百度图片查找相应的图片,通过nodejs保存到本地文件夹
    基于vue 2.X和高德地图的vue-amap组件获取经纬度
    获取Class对象的三种方式
    java中的回调机制的理解(小例子)
    URL和URI的区别
    java web开发中各类地址的写法
    ListView性能
    Android中的Handler, Looper, MessageQueue和Thread对应关系
    java几种常用的算法
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14199148.html
Copyright © 2011-2022 走看看