命令模式--设计模式(一)
2015-05-04 00:00
459 查看
摘要: * 《设计模式》权威定义:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
* Command命令模式是一种对象行为型模式,它主要解决的问题是:在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”的问题。
* 有时我们必须向某对象提交请求,但并不知道关于被请求的操作或请求的接受者的任何信息,此时无法抵御变化的紧耦合是不合适的。 如:需要对行为进行“记录、撤销/重做、事务”等处理。我们所要做的是将依赖关系转化,将紧耦合变为松耦合。
WikiPedia 维基百科定义:
command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
* Command命令模式是一种对象行为型模式,它主要解决的问题是:在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”的问题。
* 有时我们必须向某对象提交请求,但并不知道关于被请求的操作或请求的接受者的任何信息,此时无法抵御变化的紧耦合是不合适的。 如:需要对行为进行“记录、撤销/重做、事务”等处理。我们所要做的是将依赖关系转化,将紧耦合变为松耦合。
WikiPedia 维基百科定义:
command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class CommandPattern { private int state; public CommandPattern(Integer state) { this.state = state; } public int addOne(Integer a){ return state = state + a.intValue(); } public int addTwo(Integer one,Integer two){ return state = state + one.intValue() + two.intValue(); } static class Command{ private Object receiver; private Method action; private Object[] args; public Command(Object object,String methodName,Object[] args) { this.receiver = object; this.args = args; Class[] classTypes = new Class[args.length]; for(int i=0;i<args.length;i++){ classTypes[i] = args[i].getClass(); } try { action = object.getClass().getMethod(methodName, classTypes); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } public Object execute(){ try { return action.invoke(receiver, args); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; } } public static void main(String[] args) { System.out.println(new CommandPattern(0).addOne(3)); System.out.println(new CommandPattern(0).addTwo(4,5)); System.out.println("------------------------------------"); Command command1 = new Command(new CommandPattern(0),"addOne",new Object[]{3}); Command command2 = new Command(new CommandPattern(0),"addTwo",new Object[]{4,5}); System.out.println(command1.execute()); System.out.println(command2.execute()); } }
相关文章推荐
- 设计模式之单例模式、命令模式浅析
- 深入浅出设计模式——从球赛中悟命令模式
- 设计模式-命令模式
- 14.Javascript设计模式之命令模式----Command
- 设计模式_命令模式
- 设计模式-命令模式(行为模式)
- c++设计模式(命令模式)
- 23种设计模式(11):命令模式在MVC框架中的应用
- Java设计模式:命令模式(Command)
- 设计模式之Command(命令)
- 设计模式观后(c++还原之十二 命令模式)
- 设计模式—命令、职责链及解释器模式
- C++设计模式之十四:Command(命令)
- 深入浅出设计模式之命令模式、适配器模式、外观模式
- java学习笔记-设计模式19(命令模式)
- 设计模式笔记之---命令模式
- java设计模式_命令模式 两个不同风格的实现
- 设计模式:命令模式(Command Pattern)
- 设计模式之命令(Command)模式详解
- Java设计模式:六、命令模式