Design patterns not mentioned in Gof's writings (2): Interceptor

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/west-link/archive/2011/06/22/2086591.html

The Interceptor mode gives us a way to intercept the method call or message, the whole process is automatic and transparent, and here is a simple interceptor:

You can see that the interceptor can access the input parameters of the method call and return the results, so that the interceptor can do more things, such as: 1, verify that the input parameters are correct 2, secretly modify the value of the parameters, such as automatic conversion of parameter types 3, Dependency injection 4, modify the contents of the returned results, format, etc.

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

public class action{
Iterator for Interceptor Collection
Private Iterator interceptors;
Input parameters
Private Parameter param;
return results
private result result;
constructor function
Public Action (Parameter param);
This method is what we want to intercept (see below for specific implementations)
Public Result invoke ();
Other...
}

The following declares an interceptor interface:

public interface interceptor{ public Result intercept (Action action); }

We may think of the following interception methods:

/// First intercept before method call, can process input parameters intercept (param);// call method result = action.invoke (param); < Span style= "color: #008000;" >// intercept after the method call, Can handle return results intercept (result);

The disadvantage of this approach is that the invocation of the method cannot be completely controlled, such as the call to the method cannot be skipped, the result is returned directly, the state inside the object cannot be changed, and so on.

The following implementations can also intercept the method call and have the ability to fully control the object

public Result Invoke () { if(Interceptors.hasnext ()) { Interceptor Interceptor = interceptors.next (); result = interceptor.intercept (this); }}

Here are some specific examples of interception:

Parameter Legality Verification Interceptor
Public Paraminvalidator implements interceptor{
Public Result intercept (action action) {
Paramemter param = Action.getparam ();
Ensure that the parameter is not NULL
if (param = = null) {
Addmessege ("param is null!");
To create a default parameter object
Action.setparam (New Paramemter ());
}
Continue calling procedure
return Action.invoke ();
}
}

In general, such interceptors should be placed at the front of the interceptor set, so the order in which the interceptors are executed is more important! This depends on the specific implementation requirements.

If the requirements are required: null when the parameter returns a null result, 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;
}
Continue calling procedure
return Action.invoke ();
}

So, in the implementation of all interceptors, action.invoke () The position of this line of code is very important, by controlling it we can intercept before and after the call, not even call it!

public interface moneyaware{
public void Setmoney (int. money);
}
Dependency Injection Interceptors
Public Dependencyinjector implements interceptor{
Public Result intercept (action action) {
If it implements the Moneyaware interface, then we'll inject 5 cents into it.
if (action instanceof Moneyaware) {
Action.setmoney (5);
}
Continue calling procedure
return Action.invoke ();
}
}

As you can see from this interceptor, we can modify the internal state of the intercepted object.

Result formatting interceptors
Public Resultformater implements interceptor{
Public Result intercept (action action) {
First call the method you want to intercept
result = Action.invoke ();
Formatting the results
FormatResult (result);
return results
return result;
}
}

This interceptor is the interception after the method call, so the interceptor is executed at the bottom of the call stack, and it is executed when the other interceptor finishes executing!

Design patterns not mentioned in Gof's writings (2): Interceptor

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.