public class Main {
public static void main(String[] args) {
new ActionInvocation().invoke();
}
}
===========================
import java.util.ArrayList;
import java.util.List;
public class ActionInvocation {
List<Interceptor> interceptors = new ArrayList<Interceptor>();
int index = -1;
Action action = new Action();
public ActionInvocation(){
this.interceptors.add(new FristInterceptor());
this.interceptors.add(new SecondInterceptor());
}
public void invoke(){
index++;
if(index >= this.interceptors.size()){
action.execute();
}else{
this.interceptors.get(index).interceptor(this);
System.out.println(this.getClass().getSimpleName());
}
}
}
==================
public interface Interceptor {
public void interceptor(ActionInvocation invocation);
}
===============================================
public class FristInterceptor implements Interceptor {
@Override
public void interceptor(ActionInvocation invocation) {
// TODO Auto-generated method stub
System.out.println(1);
invocation.invoke();
System.out.println(-1);
}
}
====================================================
public class SecondInterceptor implements Interceptor {
@Override
public void interceptor(ActionInvocation invocation) {
// TODO Auto-generated method stub
System.out.println(2);
invocation.invoke();
System.out.println(-2);
}
}
======================================================
public class Action {
public void execute(){
System.out.println("execute !");
}
}