Java design pattern--responsibility chain model

Source: Internet
Author: User

design mode – Responsibility chain Model Introduction:

Responsibility chain mode is a dynamic behavior pattern, there are multiple objects, each object has its next reference, linked together to form a chain, the object to be processed to this chain, the chain is passed, and the object to be processed does not know which object is being processed on the chain, This allows us to dynamically increase the number of objects on the chain and assign responsibilities.

Pure liability chain and non-pure liability chain

Responsibility chain is divided into two kinds, pure responsibility chain and non-pure responsibility chain.
The pure responsibility chain is to be processed object request in the processing object handler chain pass, each processing object handler processing request of part, to the final processing complete, the non-pure responsibility chain is to handle the object request in the processing object handler chain pass, Each processing object handler either takes the request out of the process, or does not handle it, leaving it to the next handler.

implementation (non-pure chain of responsibility):

Scene:
Must meet certain conditions when the processing object (handler) to handle the object to be processed (request), otherwise passed in the responsibility chain, the code I use is the ability value Abilitytovalue,handler satisfies the ability value is greater than the request can be processed, Otherwise, it is passed down in the chain of responsibility.

Handler.java

/** * Created by WWH on 15-7-30. * * Public Abstract  class Handler {    / * Capacity value * /    protected intAbilitytovalue =0;/ * Hold a reference to the next object * /    protectedHandler Nexthandler;/ * Specific handling methods * /     Public Abstract void HandleRequest(intVaule);/ * Get a reference to the next object * /     PublicHandlerGetnexthandler(){if(Nexthandler! =NULL) {returnNexthandler; }Else{/ * At the end of the chain of responsibility, failed to process * *System.out.println ("The object to be disposed of is a chain of responsibility ..."); }return NULL; }/ * Set subsequent processing objects * /     Public void Setnexthandler(Handler Nexthandler) {if(Nexthandler! =NULL) { This. Nexthandler = Nexthandler; }Else{System.out.println ("reach the end, set the complete responsibility chain"); }}}class Handlerrequest_one extends handler{/ * Set Capacity value * /Handlerrequest_one (intATV) { This. Abilitytovalue = ATV; }/ * Specific processing request method * /    @Override     Public void HandleRequest(intValue) {/ * If the ability value of the processing object is within the range of 100-200, then the request is processed, otherwise it will not be able to process the request and give it to the next handler * /        if(Abilitytovalue > Value) {System.out.println ("Handlerrequest_one processing Request"); }Else{System.out.println ("Handlerrequest_one let go of the request, pass to the next handler");if(Getnexthandler ()! =NULL) Getnexthandler (). HandleRequest (value); }}}class Handlerrequest_two extends handler{/ * Set Capacity value * /Handlerrequest_two (intATV) { This. Abilitytovalue = ATV; }@Override     Public void HandleRequest(intValue) {/* If the ability value of the processing object is within the range of 200-400, then process this request, * otherwise not able to process the request, to the next handler * /        if(Abilitytovalue > Value) {System.out.println ("Handlerrequest_two processing Request"); }Else{System.out.println ("Handlerrequest_two let go of the request, pass to the next handler");if(Getnexthandler ()! =NULL) Getnexthandler (). HandleRequest (value); }    }}

request.java

/** * Created by WWH on 15-7-30. * * Public  class Request {     Public intablity; Request (intablity) { This. ablity = ablity; } Public Static void Main(string[] args)throwsexception{Handler HD1 =NewHandlerrequest_one ( -); Handler hd2 =NewHandlerrequest_two ( -); Hd1.setnexthandler (HD2);/ * Situation 1 * /Request req1 =NewRequest ( -);        Hd1.handlerequest (req1.ablity); System.out.println ("-------------------------");/ * Situation 2 * /Request REQ2 =NewRequest ( -);        Hd1.handlerequest (req2.ablity); System.out.println ("-------------------------");/ * Situation 3 * /Request REQ3 =NewRequest ( -);    Hd1.handlerequest (req3.ablity); }}
Results:

The chain of responsibility model is used in Java a lot, in addition to the most recently seen in the Netty in the pipeline, there is a servlet in the filters (filter), Struts2 Interceptor (Interceptor).

pros and Cons:

Advantages:
Requester-to-processor code separation: reduces the coupling degree.
Cons: The chain of responsibility is like a linked list, and efficiency can be problematic. There is no guarantee that the request will be received

贴张网片

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java design pattern--responsibility chain model

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.