运行主类
Main.java
public class Main {
public static void main(String[] args) {
new ActionInterceptor().invoke() ;
}
}
/********************************/
拦截器核心
ActionInterceptor.java
import java.util.ArrayList;
import java.util.List;
public class ActionInterceptor {
List<Inte> intes = new ArrayList<Inte>() ;
int index = -1 ;
public ActionInterceptor() {
intes.add(new FistInte()) ;
intes.add(new SecondInte()) ;
}
public void invoke() {
index++ ;
if(index >= intes.size()) {
new Action().execute() ;
}else {
this.intes.get(index).inte(this) ;
}
}
}
/********************************/
接口:与拦截器之间的通讯接口
Inte.java
public interface Inte {
void inte(ActionInterceptor actioninterceptor) ;
}
/********************************/
接口使用类
FistInte.java
public class FistInte implements Inte {
@Override
public void inte(ActionInterceptor actioninterceptor) {
System.out.println(1) ;
actioninterceptor.invoke() ;
System.out.println(-1) ;
}
}
SecondInte.java
public class SecondInte implements Inte {
@Override
public void inte(ActionInterceptor actioninterceptor) {
System.out.println(2) ;
actioninterceptor.invoke() ;
System.out.println(-2) ;
}
}
/********************************/
动作类
Action.java
public class Action {
public void execute() {
System.out.println("execute!") ;
}
}