1. Interceptor interface
Public Interface Interceptor { void Interceptor (actioninvocation invocation);}
2. Make 3 implementations of the interface
Public classFirstinterceptorImplementsInterceptor { Public voidInterceptor (Actioninvocation invocation) {System.out.println (1); Invocation.invoke (); System.out.println (-1); }} Public classSecondinterceptorImplementsinterceptor{ Public voidInterceptor (Actioninvocation invocation) {System.out.println (2); Invocation.invoke (); System.out.println (-2); }} Public classThirdinterceptorImplementsinterceptor{ Public voidInterceptor (Actioninvocation invocation) {System.out.println (3); Invocation.invoke (); System.out.println (-3); }}
3. An Action object
Public class Action { publicvoid execute () { System.out.println ("Execute executed!!! "); }}
4.ActionInvocation initializes the implementation of 3 interceptors to the list, simulating the interceptor stack
1 Public classactioninvocation {2List<interceptor> interceptors =NewArraylist<interceptor>();3 intindex =-1;4Action A =NewAction ();5 6 Publicactioninvocation () {7 This. Interceptors.add (Newfirstinterceptor ());8 This. Interceptors.add (Newsecondinterceptor ());9 This. Interceptors.add (Newthirdinterceptor ());Ten } One Public voidInvoke () { Aindex++; - - if(Index >= This. Interceptors.size ()) { the A.execute (); -}Else { - This. Interceptors.get (Index). Interceptor ( This); - } + } -}
5. Running
Public class Main { publicstaticvoid main (string[] args) { new actioninvocation (). Invoke (); }}
6. Results
1
2
3
Execute executed!!!
-3
-2
-1
A small example of the implementation of the STRUTS2 analog interceptor