命令模式
设计模式的意义在于:面向业务内容、业务数据结构和系统架构,高内聚低耦合、优雅的将平面逻辑立体化。
1 package designPattern; 2 /** 3 * 命令模式 4 * @author Administrator 5 */ 6 public class B10_CommandTest { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 Receiver rec = new Receiver(); 13 Command cmd = new CommandImpl(rec); 14 Invoker i = new Invoker(); 15 i.setCommand(cmd); 16 i.execute(); 17 } 18 } 19 //command 声明执行操作的接口。 20 abstract class Command { 21 protected Receiver receiver; 22 public Command(Receiver receiver) { 23 this.receiver = receiver; 24 } 25 public abstract void execute(); 26 } 27 //concreteCommand 将一个接收者对象绑定于一个动作。调用接收者相应的操作,以实现Execute。 28 class CommandImpl extends Command { 29 public CommandImpl(Receiver receiver) { 30 super(receiver); 31 } 32 33 public void execute() { 34 receiver.receive(); 35 } 36 } 37 //invoker 要求该命令执行这个请求。 38 class Invoker { 39 private Command command; 40 41 public void setCommand(Command command) { 42 this.command = command; 43 } 44 45 public void execute() { 46 command.execute(); 47 } 48 } 49 //receiver 50 class Receiver { 51 52 public void receive() { 53 System.out.println("This is Receive class!"); 54 } 55 }
环境:JDK1.6,MAVEN,tomcat,eclipse
源码地址:https://files.cnblogs.com/files/xiluhua/designPattern.rar
欢迎亲们评论指教。