现在的位置: 首页 > 综合 > 正文

大话设计模式之烤羊肉串[CommandPattern]

2012年06月03日 ⁄ 综合 ⁄ 共 1893字 ⁄ 字号 评论关闭

using System.Collections;
using System.Collections.Generic;
namespace Roboth.Pattern.Command
{
    abstract public class Command
    {
        protected Barbecuer bb;
        public Command(Barbecuer b)
        {
            this.bb = b;
        }
        abstract public void ExecuteCommand();
    }
    public class BakeMutton : Command
    {
        public BakeMutton(Barbecuer b)
            : base(b)
        { }
        public override void ExecuteCommand()
        {
            //throw new System.Exception("The method or operation is not implemented.");
            bb.BakeMutton();
           
        }
    }
    public class BakeChickenWing : Command
    {
        public BakeChickenWing(Barbecuer b)
            : base(b)
        { }
        public override void ExecuteCommand()
        {
            //throw new System.Exception("The method or operation is not implemented.");
            bb.BakeChickenWing();
        }
    }
    public class Barbecuer
    {
        public void BakeMutton()
        { System.Console.WriteLine("烤了一串羊肉"); }
        public void BakeChickenWing()
        { System.Console.WriteLine("烤了一串鸡翅"); }
    }

    public class Waiter
    {
        private IList<Command> cmdList = new List<Command>();
        public void SentOrder(Command cmd)
        {
            cmdList.Add(cmd);
            System.Console.WriteLine("增加订单:"+cmd.ToString()+" 时间: "+System.DateTime.Now.ToShortDateString());
        }
        public void Notify()
        {
            foreach (Command cm in cmdList)
            { cm.ExecuteCommand(); }
        }
    }
    class app
    {
        static void Main()
        {
            //prepare store
            Barbecuer bb = new Barbecuer();
           
            Command cmd1 = new BakeChickenWing(bb);
            Command cmd11 = new BakeChickenWing(bb);
            Command cmd2 = new BakeMutton(bb);

            Waiter girl = new Waiter();

            girl.SentOrder(cmd1);
            girl.Notify();
            girl.SentOrder(cmd11);
            girl.Notify();
            girl.SentOrder(cmd2);
            girl.Notify();

            System.Console.Read();
        }
    }
}

抱歉!评论已关闭.