ブロッキングシミュレーション

1708 ワード


public class Main {
	public static void main(String[] args) {
		new ActionInvocation().invoke();
	}
}




public interface Interceptor { //    
	public void intercept(ActionInvocation invocation) ;
}




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

public class ActionInvocation {
	List<Interceptor> interceptors = new ArrayList<Interceptor>(); //          
	int index = -1;
	Action a = new Action();

	public ActionInvocation() {
		this.interceptors.add(new FirstInterceptor()); //       
		this.interceptors.add(new SecondInterceptor());

	}

	public void invoke() {
		index++;
		if (index >= this.interceptors.size()) { //     
			a.execute();
		} else {
			this.interceptors.get(index).intercept(this); // index  interceptors  intercept
		}
	}
}




public class FirstInterceptor implements Interceptor { //   Interceptor  

	public void intercept(ActionInvocation invocation) {
		System.out.println(1);
		invocation.invoke();
		System.out.println(-1);
	}

}




public class SecondInterceptor implements Interceptor {//   Interceptor  

	public void intercept(ActionInvocation invocation) {
		System.out.println(2);
		invocation.invoke();
		System.out.println(-2);
	}

}




public class Action {
	public void execute() {
		System.out.println("execute!");
	}
}