再次 鬆耦合

再次 鬆耦合

再次 鬆耦合

再次 鬆耦合

public class Waiter
{

private IList<Command> orders = new List<Command>();

public void SetOrder(Command command)
{

    if (command.ToString() == “命令模式.BakeChickenWingCommand”) 
    {

        Console.WriteLine(”服務生:雞翅沒有了,請點別的燒烤。”);
    }
    else
    {
        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();
    }
}

}

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.SetOrder(bakeMuttonCommand2);

girl.SetOrder(bakeChickenWingCommand1);

girl.Notify();

Console.Read();

}