xwork的command模式 .

转载

http://blog.csdn.net/flyforlove/article/details/1661965

 

xworkcommand模式中的几个类

命令的发出者:ActionProxy

命令的传送者:ActionInvaction

命令的执行者:Action

 

当然,xwork的内部调用没有这么简单,使用xml配置文件来联系各个类。

现在用程序模拟以下xworkcomman模式工作方式。

 

首先是ActionProxy

ActionProxy.java

 

publicinterface ActionProxy {

public String execute() throws Exception;

}

 

ActionProxy是命令的发出着,也就是说client并不是直接让Action执行的,而是让ActionProxy来代理执行(虽然这儿看起来有点儿像代理模式,但是不是).

下面是ActionProxy的实现

package jp.co.wqf;

 

publicclass ActionProxyImpl implements ActionProxy {

 

Invocation invocation;

public ActionProxyImpl(Invocation invocation) {

this.invocation = invocation;

}

public String execute() throws Exception {

returninvocation.invoke();

}

 

}

要注入一个Invocation让其来传送命令。

 

接下来是ActionInvocation

ActionInvocation.java

public interface Invocation {

public String invoke() throws Exception;

}

定义一个调用方法。

它的实现为

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Map;

 

publicclass InvocationImpl implements Invocation {

Iterator interceptors;

Action action;

Map resultMap;

booleanexecuted = false;

String resultCode;

public InvocationImpl(ArrayList interceptors, Action action, Map resultMap){

this.interceptors = interceptors.iterator();

this.action = action;

this.resultMap = resultMap;

}

public String invoke() throws Exception {

if(executed){

thrownew Exception("Action has been execute!");

}

if(interceptors.hasNext()) {

Interceptor interceptor = (Interceptor) interceptors.next();

interceptor.intercept(InvocationImpl.this);

}else{

resultCode = action.exectute();

}

if(!executed){

Result result = (Result) resultMap.get(resultCode);

if(result!=null)

result.execute();

executed = true;

}

returnresultCode;

}

 

}

这是一个比较主要的类,很多处理都是通过这儿来中转的,首先它通过注入递归调用来实现了方法拦截,然后再调用Action来执行真正的处理,并根据处理结果来调用不同的结果处理类(Result

 

 

最后是Action接口

Action.java

publicinterface Action {

public String exectute();

}

Action接口里定义了一个execute方法,用来执行某种操作,例如,数据库的CRUD各个操作等,用一个String的返回值来表示执行结果,比如“success”,“error”等。实际上xwork支持任何的执行方法,不仅限于execute,只要在xwork.xml文件中指定执行哪个方法就可以了

 

以下是Action接口的实现类,这儿返回一个“error”字符串。

publicclass ActionImpl implements Action {

 

public String exectute() {

System.out.println("Action Execute");

//return "success";

return"error";

}

 

}

 

另外还有InterceptorResult接口,实现类。

Interceptor.java

public interface Interceptor {

publicvoid intercept(Invocation invocation) throws Exception;

}

InterceptorImpl.java

public class InterceptorImpl implements Interceptor {

private String name;

public InterceptorImpl(String name){

this.name = name;

}

publicvoid intercept(Invocation invocation) throws Exception {

System.out.println(name+", start");

invocation.invoke();

System.out.println(name+", end");

}

 

}

Result.java

public interface Result {

publicvoid execute();

}

ResultImpl.java

public class ResultImpl implements Result {

private String name;

public ResultImpl(String name) {

this.name = name;

}

publicvoid execute() {

System.out.println("Result "+this.name+" execute!");

}

 

}

测试类如下:

public class Test {

 

/**

*@paramargs

*@throwsException

*/

publicstaticvoid main(String[] args) throws Exception {

// TODO Auto-generated method stub

ArrayList interceptors = new ArrayList();

for (int i = 0; i < 5; i++) {

Interceptor inter = new InterceptorImpl("Interceptor_"+i);

interceptors.add(inter);

}

Result result = null;

Map resultMap = new HashMap();

result = new ResultImpl("success");

resultMap.put("success", result);

result = new ResultImpl("error");

resultMap.put("error", result);

 

Action action = new ActionImpl();

Invocation invocation = new InvocationImpl(interceptors, action, resultMap);

 

ActionProxy actionProxy = new ActionProxyImpl(invocation);

actionProxy.execute();

}

 

}

 

要点:

1.comman模式

2.IOC注入

3.递归调用(使用递归调用来实现方法拦截是个比较新颖的方法)

你可能感兴趣的:(command)