Java design pattern--responsibility chain model

Source: Internet
Author: User

design mode – Responsibility chain Model Introduction:

The responsibility chain pattern is a dynamic behavior pattern with multiple objects, each of which has its own reference. Link up to form a chain. The object to be processed is passed on to this chain, and the object being processed does not know which object is being processed by the chain, which allows us to dynamically add the objects on the chain and assign responsibility.

Pure liability chain and non-pure liability chain

The responsibility chain is divided into two types. Pure liability chain and non-pure responsibility chain.
The pure responsibility chain is the object to be processed request is passed on the processing object handler chain, each processing object handler processing request part, finally processing completes. The non-pure liability chain is the request of the object to be processed is passed on the handler chain of the processing object, each processing object handler or takes out all the processing of the request. Or do not deal with it. Left to the next handler.

implementation (non-pure chain of responsibility):

Scene:
The processing object (handler) has the ability to handle a pending object (request) When certain conditions must be met. Otherwise, in the chain of responsibility, the code I use is the ability value Abilitytovalue,handler satisfaction capacity 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;/ * Detailed handling method * /     Public Abstract void HandleRequest(intVaule);/ * Get a reference to the next object * /     PublicHandlerGetnexthandler(){if(Nexthandler! =NULL) {returnNexthandler; }Else{/ * Reach 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 ("At the end, set the chain of responsibility."); }}}class Handlerrequest_one extends handler{/ * Set Capacity value * /Handlerrequest_one (intATV) { This. Abilitytovalue = ATV; }/ * Verbose handling of request method * /    @Override     Public void HandleRequest(intValue) {/ * Assume that the ability to handle objects is within the range of 100-200. Then handle this request, otherwise you will not be able to process this 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. Passed 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) {/* Assuming that the capability value of the processing object is within the range of 200-400, then process this request, * otherwise you will not be able to process this request and give it 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. Passed 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 responsibility chain model is very much used in Java, in addition to the recent pipeline in the Netty, there are filters in the servlet (filter), Struts2 interceptors (Interceptor).

Advantages and Disadvantages:

Strengths:
Requester-to-processor code separation: reduced coupling.
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

贴张网片

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.