Design Patterns not mentioned in the gof book (2): Interceptor

Source: Internet
Author: User

The interceptor mode provides an automatic and transparent way to intercept method calls or messages. The following is a simple Interceptor:

As you can see, the interceptor can access the input parameters and returned results of method calls. In this way, the interceptor can do more things, such:
1. Verify that the input parameters are correct.
2. Secretly modify the parameter values, such as automatic conversion of parameter types.
3. Dependency Injection
4. modify the content and format of returned results.

The following is a class that contains the methods we want to intercept:

 Public     Class  Action {
// Iterator of the interceptor set
Private Iterator interceptors;
// Input parameters
Private Parameter Param;
// Returned results
Private Result result;
// Constructor
Public Action (parameter PARAM );
// This method is to be intercepted (For details, refer to the following)
Public Result invoke ();
// Other...
}

The following declares an interceptor interface:

Public InterfaceInterceptor {
PublicResult intercept (Action action );
}

We may think of the following interception methods:

//Intercept the request before calling the method to process input parameters.
Intercept (PARAM );
//Call Method
Result=Action. Invoke (PARAM );
//You can intercept the method after it is called to process the returned results.
Intercept (result );

The disadvantage of this method is that you cannot completely control the call of a method. For example, you cannot skip the call of a method and directly return the result, or change the internal status of the object.

The following implementation can also achieve the frontend and backend interception of method calls, and has the ability to fully control this object

PublicResult invoke (){
If(Interceptors. hasnext ()){
Interceptor interceptor=Interceptors. Next ();
Result=Interceptor. Intercept (This);
}
}

Here are some examples of interception:

 //  Parameter validity verification interceptor  
Public Paraminvalidator Implements Interceptor {
Public Result intercept (action Action ){
Paramemter Param = Action. getparam ();
// Make sure that the parameter is not null
If (Param = Null ){
Addmessege ( " Param is null! " );
// Create a default parameter object
Action. setparam ( New Paramemter ());
}
// Continue call Process
Return Action. Invoke ();
}
}

Generally, this type of interceptor should be placed at the beginning of the interceptor set. Therefore, the sequence in which the interceptor is executed is important! This depends on the specific implementation requirements.

If there is a requirement: If the parameter is null, a null result is returned directly to stop the call process. Then we can change the above method to this:

  Public   result intercept (action Action) {
paramemter Param = action. getparam ();
If (Param = null ) {
return null ;
}< br> /// continue calling
return action. invoke ();
}

Therefore, in the implementation of all interceptors, the line action. Invoke ()CodeThe location or whether it is very important. By controlling it, we can intercept before and after the call, or even not call it!

 Public     Interface  Moneyaware {
Public Void Setmoney ( Int Money );
}
// Dependency injection interceptor
Public Dependencyinjector Implements Interceptor {
Public Result intercept (action Action ){
// If it implements the moneyaware interface, we will inject 5 cents into it.
If (Action Instanceof Moneyaware ){
Action. setmoney ( 5 );
}
// Continue call Process
Return Action. Invoke ();
}
}

We can see from this interceptor that we can modify the internal status of the intercepted object.

 //  Result formatting interceptor  
Public Resultformater Implements Interceptor {
Public Result intercept (action Action ){
// Call the method to intercept
Result = Action. Invoke ();
// Format the result
Formatresult (result );
// Returned results
Return Result;
}
}

This interceptor is used to intercept a method call. Therefore, this interceptor is executed at the bottom of the Call Stack. It is executed only after other interceptors are executed!

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.