您的位置:首页 > 其它

命令模式--设计模式(一)

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.

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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息