C # simulate interceptor implementation

Source: Internet
Author: User

During SSH project development, most of the functions of Struts are implemented through the interceptor. In terms of programming, interceptor is also the implementation of AOP. Here, we use the C # code to simulate a simple interceptor. First, let's look at A piece of code. This code is an error code, because it will be stuck in endless loop calls [csharp] public class A {B B B = new B (); public void invoke () {B. method (this) ;}} public class B {public void mehtodd (A a) {. invoke () ;}} In fact, the implementation of the interceptor is very similar to this method. struts does a good job in ActionInvoke, so the entire process is very logical. As you can see, in the ActionInvoke invoke method, index plays a very important shunting role, avoiding endless mutual calls between Interceptor and. The following is the complete code for simulating the interceptor [csharp] class Program {static void Main (string [] args) {Action a = new Action (); ActionInvocation invoker = new ActionInvocation (); invoker. invoke (); Console. readLine () ;}} public class Action {public void Execute () {Console. writeLine ("Action invoked") ;}} public interface Interceptor {void Intercept (ActionInvocation actionInvocation);} public class Interceptor1: Interceptor {Public void Intercept (ActionInvocation actionInvocation) {Console. writeLine ("======= * 1"); actionInvocation. invoke (); Console. writeLine ("======= *-1") ;}} public class Interceptor2: Interceptor {public void Intercept (ActionInvocation actionInvocation) {Console. writeLine ("======= * 2"); actionInvocation. invoke (); Console. writeLine ("= *-2");} public class ActionInvocation {List <Intercep Tor> interceptors = new List <Interceptor> (); int index =-1; Action action = null; public ActionInvocation (Action action) {this. action = action; this. interceptors. add (new Interceptor1 (); this. interceptors. add (new Interceptor2 ();} public void invoke () {index ++; if (index> = this. interceptors. count) {action. execute ();} else {this. interceptors [index]. intercept (this) ;}}then use an image to perform a simple score on the process. Analyze step 1, index = 0, call Interceptor1's Intercept () method, output = * 1Step2, call the invoke method of actionInvoke step 3, because the index is 1 at this time, the Intercept () method of Interceptor2 is called, and the output is = * 2Step4. In the Intercept () method of Interceptor2, return to the invoke method of actionInvoke and execute action. execute () Step5, then Execute the output command in Interceptor2 Intercept (), output = *-2Step6, return to the call at the previous layer, return to Interceptor1 Intercept () and the output is *-1. In general, when Interceptor1 is executed from invoke () and the invoke method is called again, an Interceptor2 is wrapped in the execution interval of Interceptor1. After Interceptor2 is complete, it will continue to return to Interceptor1 to execute the remaining logic, which is the output string.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.