学习:Java设计模式—Command2

自从上次project中使用struts1.1以后,对于开源的框架,就再也没有使用过。

struts2开始就和webwork没有什么区别了,想了想还是看一下webwork吧,webwork是基于xwork的,而xwork是独立于webcontainer而存在的,那么就从xwork开始。

xwork是构筑与command模式的。

对于command模式的讲解,请看。

http://www.dofactory.com/Patterns/PatternCommand.aspx

 

xworkcommand模式中的几个类

命令的发出者:ActionProxy

命令的传送者:ActionInvaction

命令的执行者:Action

 

类图如下:


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

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

 

首先是ActionProxy

ActionProxy.java

 

public interface ActionProxy {

public String execute() throws Exception;

}

 

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

下面是ActionProxy的实现

package jp.co.wqf;

 

public class ActionProxyImpl implements ActionProxy {

 

Invocation invocation;

public ActionProxyImpl(Invocation invocation) {

this.invocation = invocation;

}

public String execute() throws Exception {

return invocation.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;

 

public class InvocationImpl implements Invocation {

Iterator interceptors;

Action action;

Map resultMap;

boolean executed = 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){

throw new 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;

}

return resultCode;

}

 

}

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

 

 

最后是Action接口

Action.java

public interface Action {

public String exectute();

}

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

 

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

public class ActionImpl implements Action {

 

public String exectute() {

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

//return "success";

return "error";

}

 

}

 

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

Interceptor.java

public interface Interceptor {

public void intercept(Invocation invocation) throws Exception;

}

InterceptorImpl.java

public class InterceptorImpl implements Interceptor {

private String name;

public InterceptorImpl(String name){

this.name = name;

}

public void intercept(Invocation invocation) throws Exception {

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

invocation.invoke();

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

}

 

}

Result.java

public interface Result {

public void execute();

}

ResultImpl.java

public class ResultImpl implements Result {

private String name;

public ResultImpl(String name) {

this.name = name;

}

public void execute() {

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

}

 

}

测试类如下:

public class Test {

 

/**

* @param args

* @throws Exception

*/

public static void 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.递归调用(使用递归调用来实现方法拦截是个比较新颖的方法)

原文地址:https://www.cnblogs.com/forlina/p/2083826.html