The responsibility chain model of Java design pattern and its role in Java

Source: Internet
Author: User
Tags array length

The responsibility chain pattern is an object's behavior pattern. In the chain of responsibility, many objects are linked together by each object's reference to its next generation. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain is ultimately processing the request, which allows the system to reorganize and assign responsibilities dynamically without affecting the client.

Pure and impure chain of Duty mode

A pure chain of responsibility requires a specific handler object to choose only one of two behaviors: one is to take responsibility, but to push the responsibility to the next. A situation in which a specific processor object is not allowed to transmit responsibility after taking part of the responsibility.

In a pure chain of responsibility, a request must be received by a handler object, and in an impure chain of responsibility, a request can eventually not be received by any of the receiving end objects.

The actual example of the pure responsibility chain model is difficult to find, and the examples we see are the implementation of the impure responsibility chain model. Some people think that the impure chain of responsibility is not the responsibility chain at all, which may make sense. But in a real system, a pure chain of responsibility is hard to find. If the adherence to the chain of responsibility is not the responsibility chain model, then the chain of responsibility model will not have much significance.

The application of the responsibility chain model in Tomcat

It is well known that the filter in Tomcat is using the responsibility chain pattern, creating a filter to implement the Javax.servlet.Filter interface in addition to the corresponding configuration in the Web. xml file.

public class Testfilter implements filter{public    void DoFilter (ServletRequest request, Servletresponse response,< C1/>filterchain chain) throws IOException, servletexception {                chain.dofilter (request, response);    }    public void Destroy () {    } public    void init (Filterconfig filterconfig) throws servletexception {    }}

The results you see with the debug mode are as follows

In fact, before actually executing to the Testfilter class, it passes through a lot of Tomcat's internal classes. Incidentally, in fact, the container setting of Tomcat is also the responsibility chain mode, notice that the class is circled by the red box, from engine to host to the context until wrapper is passed through a chain request. There is a class named Applicationfilterchain in the circle of green squares, and the Applicationfilterchain class acts as an abstract processor role, while the specific processor role is played by each filter.

The first question is where does the Applicationfilterchain store all the filter?

The answer is in an array of Applicationfilterconfig objects that are saved in the Applicationfilterchain class.

What about the Applicationfilterconfig object?

Applicationfilterconfig is a filter container.

When a Web application is first launched, Applicationfilterconfig is automatically instantiated, and it reads the configured filter information from the Web application's. xml file, and then it is loaded into the container.

When I just saw that the Applicationfilterconfig array created in the Applicationfilterchain class was zero in length, when was it re-assigned?

    Private applicationfilterconfig[] Filters =         new applicationfilterconfig[0];

is when you call the AddFilter () method of the Applicationfilterchain class.

The variable n is used to record the number of filters that are in the current filter chain, and by default n equals the length of the 0,applicationfilterconfig object array equal to 0, so when the AddFilter () method is first called, the IF (n = = Filters.length), the applicationfilterconfig array length is changed. After filters[n++] = filterconfig, the variable filterconfig is placed in the Applicationfilterconfig array and the number of filters contained within the current filter chain is +1.

Where did the Applicationfilterchain addfilter () method be called?

is in the Createfilterchain () method of the Applicationfilterfactory class.

The above code can be divided into two paragraphs, 51 lines before the first paragraph, 51 lines after the second paragraph.

The primary purpose of the first paragraph is to create Applicationfilterchain objects and some parameter settings.

The main purpose of the second paragraph is to get all the filter information from the context, and then use the For loop to traverse and Invoke Filterchain.addfilter (Filterconfig); Place the filterconfig into the applicationfilterconfig array of the Applicationfilterchain object.

Where is the Createfilterchain () method of the Applicationfilterfactory class called?

is called in the Invoke () method of the Standardwrappervalue class.

  

The normal process should be this:

Call the Createfilterchain () method of the Applicationfilterchai class in the Invoke () method of the Standardwrappervalue class ——— > Call the AddFilter () method of the Applicationfilterchain class in the Createfilterchain () method of the Applicationfilterchai class ——— > Assigns a value to the Applicationfilterconfig array in the AddFilter () method of the Applicationfilterchain class.

According to the code above, you can see that the invoke () method of the Standardwrappervalue class will continue to execute the Dofilter () of the Applicationfilterchain class after the Createfilterchain () method is executed. method, and then the Internaldofilter () method is called in the Dofilter () method.

The Filter.dofilter here (request, response, this) is called the DoFilter () method in the Testfilter we created earlier. The DoFilter () method in Testfilter will continue to invoke Chain.dofilter (request, response), and this chain is actually applicationfilterchain, So the calling process goes back to calling Dofilter and calling the Internaldofilter method, so it executes until the filter inside is executed.

Because the Invoke () method is longer, it is omitted in many places.

The responsibility chain model of Java design pattern and its role in Java

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.