1. 烧烤摊vs.烧烤店

烧烤摊,行为请求者与实现者都是老板。紧耦合
烧烤店,请求者是服务员,实现者烤肉的师傅方法

2. 紧耦合的设计

A: 烤都是实现者的方法,具体怎么做都是由方法内部实现。对于服务员,他其实就是根据用户的需要,发个命令。
B: 把烤肉者类当中的方法,分别写成多个命令类,那么它们就可以被服务员来请求了。

3. 松耦合设计

图1

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("烤鸡翅!");
    }
}

A: 问题,1)应该是点完烧烤后,服务员一次通知制作;2),如果鸡翅没了,应该是服务员或烤肉串者来否决这个请求;3),需要日志记录,包括后期的统计;4),客户有可能取消一些没完全执行的命令。

4. 松耦合后

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

        //点菜完闭,通知厨房
        girl.Notify();

        Console.Read();

    }
}

//服务员
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();
        }
    }
}

//抽象命令
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("烤鸡翅!");
    }
}

5. 命令模式

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

图2

class Program
{
    static void Main(string[] args)
    {
        Receiver r = new Receiver();
        Command c = new ConcreteCommand(r);
        Invoker i = new Invoker();

        // Set and execute command 
        i.SetCommand(c);
        i.ExecuteCommand();

        Console.Read();

    }
}

abstract class Command
{
    protected Receiver receiver;

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

    abstract public void Execute();
}

class ConcreteCommand : Command
{
    public ConcreteCommand(Receiver receiver)
        :
        base(receiver) { }

    public override void Execute()
    {
        receiver.Action();
    }
}

class Receiver
{
    public void Action()
    {
        Console.WriteLine("执行请求!");
    }
}

class Invoker
{
    private Command command;

    public void SetCommand(Command command)
    {
        this.command = command;
    }

    public void ExecuteCommand()
    {
        command.Execute();
    }
}

6. 命令模式作用

B: 1)它能够较容易地设计一个命令队列;第二,在需求的情况下,可以容易地将命令记入日志;3),允许接收请求的一方决定是否要否决请求。
A: 第四,可以容易地实现对请求的撤销和重做。还有就是,命令模式把请求一个操作的对象与知道怎么执行一个操作的对象分隔开
A: 敏捷开放原则告诉我们,不要为代码添加基于猜测的,实际不需要的功能。如果不清楚一个系统是否需要命令模式,一般不要着急去实现它,事实上,在需要的时候通过重构实现这个模式并不困难,只有在真正需要如撤销/恢复操作等功能时,把原来的代码重构为命令模式才有意义。


yuanoung
10 声望1 粉丝

« 上一篇
桥接模式
下一篇 »
责任链模式

引用和评论

0 条评论