标签:png ack src 好的 center 不同 margin 扩展 static
命令模式 |
命令模式将请求方(Invoker)和执行方(Receiver)区分开了,扩展性也有很好的保障,其通用代码如下:
public class Invoker { //执行的命令 private Command command; public Invoker(Command command){ this.command = command; } public void execute(){ this.command.excute(); } } public abstract class Command { //抽象的执行方法 public abstract void excute(); } public class ConcreteCommand1 extends Command{ //具体的命令的接收者 private Receiver receiver; public ConcreteCommand1(){ this.receiver = new Receiver1(); } public ConcreteCommand1(Receiver receiver){ this.receiver = receiver; } @Override public void excute() { // TODO Auto-generated method stub this.receiver.doSomething(); } } public abstract class Receiver { //执行者的抽象执行方法 public abstract void doSomething(); } public class Receiver1 extends Receiver{ @Override public void doSomething() { // TODO Auto-generated method stub System.out.println("receiver1 do something"); } } public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Command command = new ConcreteCommand1(); Invoker invoker = new Invoker(command); invoker.execute(); } }
标签:png ack src 好的 center 不同 margin 扩展 static
原文地址:http://www.cnblogs.com/zhanglei93/p/6068554.html