设计模式- 命令模式

Gof定义

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。

理解

对命令模式我有一些肤浅的认识,也许是不对的。但我还是写一下。其实在我理解的命令模式很简单的。比如,A想去超市买菜,B想去超市买苹果,C想去超市买鱼。那么这时候要是有一个接收命令的人,那该多好哇。这个人接收A的命令,接收B的命令,接收C的命令,等所有人的命令都接收完后,他就开始去DO。当然如果A发过命令后,突然觉的还是 自己去买好了不买好了,那又有相应的CANCEL命令。 

CODE

    class Program
    {
        static void Main(string[] args)
        {

            Command cmd1 = new BakeChickenWingCommand();
            Command cmd2 = new BakeMuttonCommand();

            Waiter waiter = new Waiter();
            waiter.SetOrder(cmd1);
            waiter.SetOrder(cmd2);
            waiter.Notify();

            Console.WriteLine("-----------------------------------------");
            waiter.CancelOrder(cmd1);
            waiter.Notify();

            Console.ReadLine();
        }
    }
     class Waiter
    {
        private IList<Command> orders = new List<Command>();

        //设置订单
        public void SetOrder(Command command)
        {
            orders.Add(command);
            Console.WriteLine("增加订单:" + command.ToString() + "  时间:" + DateTime.Now.ToString());
        }

        //取消订单
        public void CancelOrder(Command command)
        {
            orders.Remove(command);
            Console.WriteLine("取消订单:" + command.ToString() + "  时间:" + DateTime.Now.ToString());
        }

        //通知全部执行
        public void Notify()
        {
            foreach (Command cmd in orders)
            {
                cmd.ExcuteCommand();
            }
        }
    }

    //抽象命令
    abstract class Command
    {
        abstract public void ExcuteCommand();    
    }

    //烤羊肉串命令
    class BakeMuttonCommand : Command
    {
        public override void ExcuteCommand()
        {
            Console.WriteLine("烤羊肉串");
        }   
    }

    //烤鸡翅命令
    class BakeChickenWingCommand : Command
    {
        public override void ExcuteCommand()
        {
            Console.WriteLine("烤鸡翅命令");
        }
    }

 

大话设计模式里的CODE

View Code
using System;
using System.Collections.Generic;
using System.Text;

namespace 命令模式
{
    class Program
    {
        static void Main(string[] args)
        {
            //开店前的准备
            Barbecuer boy = new Barbecuer();
            Command bakeMuttonCommand1 = new BakeMuttonCommand(boy);
            Command bakeMuttonCommand2 = new BakeMuttonCommand(boy);
            Command bakeChickenWingCommand1 = new BakeChickenWingCommand(boy);
            Waiter girl = new Waiter();

            //开门营业
            girl.SetOrder(bakeMuttonCommand1);
            girl.Notify();
            girl.SetOrder(bakeMuttonCommand2);
            girl.Notify();
            girl.SetOrder(bakeChickenWingCommand1);
            girl.Notify();

            Console.Read();

        }
    }

    //服务员
    public class Waiter
    {
        private Command command;

        //设置订单
        public void SetOrder(Command command)
        {
            this.command = command;
        }
        //通知执行
        public void Notify()
        {
            command.ExcuteCommand();
        }
    }

    //抽象命令
    public abstract class Command
    {
        protected Barbecuer receiver;

        public Command(Barbecuer receiver)
        {
            this.receiver = receiver;
        }

        //执行命令
        abstract public void ExcuteCommand();
    }

    //烤羊肉串命令
    class BakeMuttonCommand : Command
    {
        public BakeMuttonCommand(Barbecuer receiver)
            : base(receiver)
        { }

        public override void ExcuteCommand()
        {
            receiver.BakeMutton();
        }
    }

    //烤鸡翅命令
    class BakeChickenWingCommand : Command
    {
        public BakeChickenWingCommand(Barbecuer receiver)
            : base(receiver)
        { }

        public override void ExcuteCommand()
        {
            receiver.BakeChickenWing();
        }
    }

    //烤肉串者
    public class Barbecuer
    {
        public void BakeMutton()
        {
            Console.WriteLine("烤羊肉串!");
        }

        public void BakeChickenWing()
        {
            Console.WriteLine("烤鸡翅!");
        }
    }

}

可以对比一下代码的不同!!会发现我删了一些,更简单的实现了。

posted @ 2012-09-22 15:51  春天又来了  阅读(302)  评论(2编辑  收藏  举报