大话设计模式-第23章 烤羊肉串引来的思考--命令模式- 高飞网

第23章 烤羊肉串引来的思考--命令模式

2016-02-02 17:43:50.0

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


//命令类
public abstract class Command{
        protected Receiver r ;//接收者,动作的真正执行者
        public Command(Receiver r){
                this.r = r;
        }
        public abstract void execute();//命令的执行
}
public class ConcreteCommand extends Command{
        public ConcreteCommand(Receiver r){
                super(r);
        }
        public void execute(){
                r.action();//调用真正的执行者执行方法
        }
}
public class Receiver{//真正的执行动作者
        public void action(){
                System.out.println("执行请求..");

        }
}
public class Invoker{
        private Command c ;
        public void setCommand(Command c){
                this.c = c;
        }

        public void executeCommand(){
                c.execute();
        }
}

public class App{
        public static void main(String[] args){
                Receiver r = new Receiver();
                Command c = new ConcreteCommand(r);
                Invoker invoker = new Invoker();
                invoker.setCommand(c);

                invoker.executeCommand();      
        }
}


命令模式的好处:

1、它能较容易地设计一个命令队列;
2、在需要的情况下可能较容易地将命令记日志
3、允许接收请求的一方决定是否要否决请求
4、可以容易地实现对请求的撤销和重做
5、由于加进新的具体命令不影响其它的类,因此增加新的具体命令类很容易

命令模式把请求一个操作的对象与知道怎么执行一个操作的对象分割开。

烤羊肉串的例子: 


//烤羊肉串人
public class Barbecuer{
        public void bakeMutton(){
                System.out.println("烤羊肉串");
        }
        public void bakeChickenWing(){
                System.out.println("烤鸡翅");
        }
}
public abstract class Command{
        protected Barbecuer receiver;//动作执行者
        public Command(Barbecuer receiver){
                this.receiver = receiver;
        }
        //执行命令
        public abstract void executeCommand();
}
//烤羊肉串命令
public class BakeMuttonCommand extends Command{
        public BakeMuttonCommand(Barbecuer receiver){
                super(receiver);
        }
        public void executeCommand(){
                receiver.bakeMutton();
        }
}
//烤鸡翅命令
public class BakeChickenWingCommand extends Command{
        public BakeChickenWingCommand(Barbecuer receiver){
                super(receiver);
        }      
        public void executeCommand(){
                receiver.bakeChickenWing();
        }
}

import java.util.*;
public class Waiter{
        private List<Command> commands = new ArrayList<Command>();
        //设置订单
        public void setOrder(Command command){
                commands.add(command);
        }

        public void cancleOrder(Command command){
                commands.remove(command);
        }
        public void doNotify(){
                for(Command c :commands){
                        c.executeCommand();
                }
        }
}

public class App{
        public 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.doNotify();
        }
}