标签:
命令模式中有如下的角色:
(1)调用者(invoker)
(2)命令接收者(receiver)
(3)客户端(client)
(4)命令对象(command)
1 public interface Command { 2 public void execute(); 3 } 4 public class ConcreteCommand implements Command { 5 6 private Receiver receiver = null; 7 private String state; 8 9 public ConcreteCommand(Receiver receiver){ 10 this.receiver = receiver; 11 } 12 public void execute() { 13 receiver.action(); 14 } 15 } 16 17 18 public class Receiver { 19 public void action(){ 20 //真正执行命令操作的功能代码 21 } 22 } 23 24 public class Invoker { 25 private Command command = null; 26 27 public void setCommand(Command command) { 28 this.command = command; 29 } 30 31 public void runCommand() { 32 command.execute(); 33 } 34 } 35 36 public class Client { 37 public void assemble(){ 38 //创建接收者 39 Receiver receiver = new Receiver(); 40 //创建命令对象,设定它的接收者 41 Command command = new ConcreteCommand(receiver); 42 //创建Invoker,把命令对象设置进去 43 Invoker invoker = new Invoker(); 44 invoker.setCommand(command); 45 } 46 }
在Netd的命令模式中Client角色和invoker角色都为CommandListener
receiver为CommandListener中各种各种Controller,其中有
FirewallController *CommandListener::sFirewallCtrl = NULL;
与上面的命令模式不同,各种Command为CommandListener的内部类,可直接使用receiver,而不用将receiver传递给Command对象。还有一点不同的就是,这里的各种controller作为receiver没有像例子中有统一的action函数,而是直接在runCommand函数(例子中是Command类的execute函数)使用controller实现各种动作的执行。
Netd的各种命令分发路径如下:
SocketListener::runListener
->FameworkListener::onDataAvailable
-> FameworkListener::dispatchCommand
-> CommandListener::各种Cmd::runCommand
(版权所有,转载请告知)
[设计模式]Netd中的命令设计模式,NetworkManagementService中的监听者模式
标签:
原文地址:http://www.cnblogs.com/claruarius/p/5782344.html