打字猴:1.700463507e+09
1700463507
1700463508 this.command=_command;
1700463509
1700463510 }
1700463511
1700463512 //执行命令
1700463513
1700463514 public void action(){
1700463515
1700463516 this.command.execute();
1700463517
1700463518 }
1700463519
1700463520 }
1700463521
1700463522 调用者就像是一个受气包,不管什么命令,都要接收、执行!那我们来看高层模块如何调用命令模式,如代码清单15-18所示。
1700463523
1700463524 代码清单15-18 场景类
1700463525
1700463526 public class Client{
1700463527
1700463528 public static void main(String[]args){
1700463529
1700463530 //首先声明调用者Invoker
1700463531
1700463532 Invoker invoker=new Invoker();
1700463533
1700463534 //定义接收者
1700463535
1700463536 Receiver receiver=new ConcreteReciver1();
1700463537
1700463538 //定义一个发送给接收者的命令
1700463539
1700463540 Command command=new ConcreteCommand1(receiver);
1700463541
1700463542 //把命令交给调用者去执行
1700463543
1700463544 invoker.setCommand(command);
1700463545
1700463546 invoker.action();
1700463547
1700463548 }
1700463549
1700463550 }
1700463551
1700463552 一个完整的命令模式就此完成,读者可以在此基础上进行扩展。
1700463553
1700463554
1700463555
1700463556
[ 上一页 ]  [ :1.700463507e+09 ]  [ 下一页 ]