Interceptor Demon

 

package cn.itcast.chain.action;java

public class TestAction {
public String execute(){
System.out.println("test action");
return "aaa";
}
} this

----------------------------------------------接口

package cn.itcast.chain.interceptor;it

import cn.itcast.chain.invocation.ActionInvocation;
/**
* 攔截器的總的接口
* @author zd
*
*/
public interface Interceptor {
String intercept(ActionInvocation invocation);
}io

 

------------------------------------ast

package cn.itcast.chain.interceptor.impl;class

import cn.itcast.chain.interceptor.Interceptor;
import cn.itcast.chain.invocation.ActionInvocation;test

public class Test1Interceptor implements Interceptor{import

public String intercept(ActionInvocation invocation) {
System.out.println("1-start");
String result = invocation.invoke();
System.out.println("1-end");
return result;
}List

}

----------------------------------

public class Test2Interceptor implements Interceptor{

public String intercept(ActionInvocation invocation) {
System.out.println("2-start");
String result = invocation.invoke();
System.out.println("2-end");
return result;
}

}

-------------------------------------------

package cn.itcast.chain.invocation;

public interface ActionInvocation {
String invoke();
}

----------------------------

 

package cn.itcast.chain.invocation.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.itcast.chain.action.TestAction;
import cn.itcast.chain.interceptor.Interceptor;
import cn.itcast.chain.interceptor.impl.Test1Interceptor;
import cn.itcast.chain.interceptor.impl.Test2Interceptor;
import cn.itcast.chain.interceptor.impl.Test3Interceptor;
import cn.itcast.chain.invocation.ActionInvocation;

public class DefaultActionInvocation implements ActionInvocation{
/**
* 聲明瞭全部的攔截器的迭代器的形式
*/
private Iterator<Interceptor> interceptors;
private String resultCode = null;
public DefaultActionInvocation(){
/**
* 初始化攔截器
*/
init();
}

public void init(){
/**
* 建立了一個攔截器的集合
*/
List<Interceptor> inter = new ArrayList<Interceptor>();
/**
* 建立了三個具體的攔截器
*/
Test1Interceptor interceptor1 = new Test1Interceptor();
inter.add(interceptor1);
Test2Interceptor interceptor2 = new Test2Interceptor();
inter.add(interceptor2);
Test3Interceptor interceptor3 = new Test3Interceptor();
inter.add(interceptor3);
this.interceptors = inter.iterator();
}

public String invoke() {
if(interceptors.hasNext()){//判斷是否有下一個攔截器
/**
* 若是有,先返回下一個攔截器
*/
Interceptor interceptor = interceptors.next();
resultCode = interceptor.intercept(DefaultActionInvocation.this);
}else{
resultCode = executeAction();
}
return resultCode;
}

private String executeAction(){
return new TestAction().execute();
}
}

 

 

---------------------------------

package cn.itcast.chain.test;

import java.util.HashMap;

import org.junit.Test;

import cn.itcast.chain.invocation.impl.DefaultActionInvocation;

public class TestAction { @Test public void testAction(){ DefaultActionInvocation invocation = new DefaultActionInvocation(); System.out.println(invocation.invoke()); } @Test public void testMap(){ int i=1; Integer b=1; if(i==b){ System.out.println("true"); } } }

相關文章
相關標籤/搜索