using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CommandSchema { class Program { static void Main(string[] args) { Barbecue boy=new Barbecue(); Command bakeMuttonCommand=new BakeMuttonCommand(boy); Command bakeChickenWingCommand=new BakeChickenWingCommand(boy); Waiter gril=new Waiter(); gril.SetOrder(bakeMuttonCommand); gril.SetOrder(bakeChickenWingCommand); gril.Notify(); Console.ReadLine(); } } /// <summary> /// 烤羊肉串的人 /// </summary> class Barbecue { public void BakeMutton() { Console.WriteLine("开始烤羊肉串"); } public void BakeChickenWing() { Console.WriteLine("开始烤鸡翅膀"); } } /// <summary> /// 抽象命令 /// </summary> abstract class Command { protected Barbecue receiver; protected Command(Barbecue receiver) { this.receiver = receiver; } abstract public void ExecuteCommand(); } class BakeMuttonCommand:Command { public BakeMuttonCommand(Barbecue receiver) : base(receiver) { } public override void ExecuteCommand() { receiver.BakeMutton(); } } class BakeChickenWingCommand:Command { public BakeChickenWingCommand(Barbecue receiver) : base(receiver) { } public override void ExecuteCommand() { receiver.BakeChickenWing(); } } /// <summary> /// invoker /// </summary> class Waiter { private IList<Command> orders=new List<Command>(); public void SetOrder(Command command) { orders.Add(command); Console.WriteLine("增加订单:" + command + " 时间:" + DateTime.Now); } public void CancelOrder(Command command) { orders.Remove(command); Console.WriteLine("取消订单:" + command + " 时间:" + DateTime.Now); } public void Notify() { foreach (Command command in orders) { command.ExecuteCommand(); } } } }