设计模式--行为模式--命令模式
设计模式--命令模式
初看命令模式,感觉设计的比较优雅,可扩展性比较好,慢慢琢磨其中用处,感觉设计的扩展性有太强了。如果在实际使用中,需要斟酌使用。
只是这里边有一个经典的解释:把对方法的调用封装成对象调用。请体会这个设计模式的精髓核心点。
一、命令模式的基本概念:
命令模式是一种高内聚的模式,其定义为:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或记录请求日志,可以提供命令的撤销和恢复功能。
命令模式的角色:
A、Command命令角色:需要执行的所有命令都在这里声明。注意这个可以进行扩展。
B、 Receive接收者角色:该角色就是干活的角色,命令传递到这里是应该被执行的。
C、Invoker调用者角色:接收到命令,并执行命令。
D、Client,使用命令的执行的地方。
二、命令模式的核心例子:
A、Command命令角色:
1 package comm.pattern.action.command; 2 3 /** 4 * 5 * @Title: ICommand.java 6 * @Package: comm.pattern.action.command 7 * @Description: 描述该文件做什么 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:05:27 10 * 11 */ 12 public interface ICommand { 13 //实际执行 14 void execute(); 15 }
A'、Command命令实现者角色:
1 package comm.pattern.action.command; 2 3 /** 4 * 5 * @Title: ConcreteCommand.java 6 * @Package: comm.pattern.action.command 7 * @Description: 对命令的扩展,可以动态的扩充 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:10:52 10 * 11 */ 12 public class ConcreteCommand implements ICommand { 13 14 private Receiver receiver; 15 16 public ConcreteCommand(Receiver receive){ 17 this.receiver = receive; 18 } 19 20 21 @Override 22 public void execute() { 23 this.receiver.action(); 24 } 25 26 }
B、 Receive接收者角色:
1 package comm.pattern.action.command; 2 /** 3 * 4 * @Title: Receiver.java 5 * @Package: comm.pattern.action.command 6 * @Description: 具体干活 7 * @author yangzhancheng 8 * @2020年2月29日:上午11:12:19 9 * 10 */ 11 public class Receiver { 12 13 public void action() { 14 System.out.println("执行请求!"); 15 } 16 17 }
C、Invoker调用者角色:
1 /** 2 * 3 * @Title: Invoker.java 4 * @Package: comm.pattern.action.command 5 * @Description: 调用者 6 * @author yangzhancheng 7 * @2020年2月29日:上午11:12:49 8 * 9 */ 10 public class Invoker { 11 12 private ICommand command; 13 14 public void setCommand(ICommand command) { 15 this.command = command; 16 } 17 18 public void executeCommand() { 19 command.execute(); 20 } 21 }
D、Client:
1 package comm.pattern.action.command; 2 3 /** 4 * 5 * @Title: Client.java 6 * @Package: comm.pattern.action.command 7 * @Description: 用户端 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:15:02 10 * 11 */ 12 public class Client { 13 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 Receiver receiver = new Receiver(); 17 ICommand command = new ConcreteCommand(receiver); 18 19 Invoker nnvoker = new Invoker(); 20 nnvoker.setCommand(command); 21 nnvoker.executeCommand(); 22 } 23 24 }
处理结果
执行请求!
三、命令模式扩展例子:
A、Command命令角色:用来定义课程
1 package comm.pattern.action.command.ext; 2 3 /** 4 * 5 * @Title: ICommand.java 6 * @Package: comm.pattern.action.command 7 * @Description: 定义课程 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:05:27 10 * 11 */ 12 public interface ICommand { 13 //实际执行 14 void execute(); 15 }
A-1、Command命令实现者角色:体育课
1 package comm.pattern.action.command.ext; 2 3 /** 4 * 5 * @Title: ConcreteCommand.java 6 * @Package: comm.pattern.action.command 7 * @Description: 对命令的扩展,可以动态的扩充 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:10:52 10 * 11 */ 12 public class SportsCommand implements ICommand { 13 14 private SportsReceiver receiver; 15 16 public SportsCommand(SportsReceiver receive){ 17 this.receiver = receive; 18 } 19 20 21 @Override 22 public void execute() { 23 this.receiver.action(); 24 } 25 26 }
A-2、Command命令实现者角色:英语课
1 package comm.pattern.action.command.ext; 2 3 /** 4 * 5 * @Title: ConcreteCommand.java 6 * @Package: comm.pattern.action.command 7 * @Description: 对命令的扩展,可以动态的扩充 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:10:52 10 * 11 */ 12 public class EnglishCommand implements ICommand { 13 14 private EnglishReceiver receiver; 15 16 public EnglishCommand(EnglishReceiver receive){ 17 this.receiver = receive; 18 } 19 20 21 @Override 22 public void execute() { 23 this.receiver.action(); 24 } 25 26 }
B、 Receive接收者角色:
B-1:体育老师
1 package comm.pattern.action.command.ext; 2 /** 3 * 4 * @Title: Receiver.java 5 * @Package: comm.pattern.action.command 6 * @Description: 具体干活 7 * @author yangzhancheng 8 * @2020年2月29日:上午11:12:19 9 * 10 */ 11 public class SportsReceiver { 12 13 public void action() { 14 System.out.println("体育课开始"); 15 } 16 17 }
B-2英语老师
1 package comm.pattern.action.command.ext; 2 /** 3 * 4 * @Title: Receiver.java 5 * @Package: comm.pattern.action.command 6 * @Description: 具体干活 7 * @author yangzhancheng 8 * @2020年2月29日:上午11:12:19 9 * 10 */ 11 public class EnglishReceiver { 12 13 public void action() { 14 System.out.println("英语课开始"); 15 } 16 17 }
C、Invoker调用者角色:
1 package comm.pattern.action.command.ext; 2 3 /** 4 * 5 * @Title: Invoker.java 6 * @Package: comm.pattern.action.command 7 * @Description: 调用者 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:12:49 10 * 11 */ 12 public class Invoker { 13 14 private ICommand command; 15 16 public void setCommand(ICommand command) { 17 this.command = command; 18 } 19 20 public void executeCommand() { 21 command.execute(); 22 } 23 }
D、Client:
1 package comm.pattern.action.command.ext; 2 3 /** 4 * 5 * @Title: Client.java 6 * @Package: comm.pattern.action.command 7 * @Description: 用户端 8 * @author yangzhancheng 9 * @2020年2月29日:上午11:15:02 10 * 11 */ 12 public class Client { 13 14 public static void main(String[] args) { 15 // 体育课 16 SportsReceiver sreceiver = new SportsReceiver(); 17 ICommand command = new SportsCommand(sreceiver); 18 19 Invoker nnvoker = new Invoker(); 20 nnvoker.setCommand(command); 21 nnvoker.executeCommand(); 22 23 24 25 // 英语课 26 EnglishReceiver ereceiver = new EnglishReceiver(); 27 command = new EnglishCommand(ereceiver); 28 29 nnvoker.setCommand(command); 30 nnvoker.executeCommand(); 31 32 } 33 34 }
运行结果
体育课开始
英语课开始
四、总结:
主要在于解耦。
浙公网安备 33010602011771号