Interceptor Demon

 

package cn.itcast.chain.action;

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

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

package cn.itcast.chain.interceptor;

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

 

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

package cn.itcast.chain.interceptor.impl;

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

public class Test1Interceptor implements Interceptor{

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

}

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

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");

}






}


}

posted @ 2018-07-20 16:07  applekingghfhfhbr  阅读(76)  评论(0编辑  收藏  举报