Design pattern--The responsibility chain mode or the duty chain mode (Chain of Responsibility (CoR))

Source: Internet
Author: User

What is a chain? Connected, one connection to the next, including multiple nodes, in fact, the chain of responsibility pattern is similar, he is a number of objects are interrelated, the next object's operation is associated with the previous object, the previous object has a method to point to other objects. The relationship between the chain of responsibility can be changed, mainly on which node the node is mainly pointed to.

Event distribution in Android is the concrete manifestation of the chain of responsibility Model:

The Ontouchevent return value setting of the view, if true, is handled by the method of its own consumption event, and if false, continues to pass to the next one.

How to implement the chain of responsibility Model:

For example, the chain of responsibility, a,b,c three objects, if a has permission to deal with their own, otherwise passed to b,b if there is no permission processing, then pass, c if there is permission to handle, then processing, otherwise not processed.

A in the logic of judgment, one is in line with the conditions, to deal with themselves, one is not in line with their own conditions, to the chain of the next object to deal with, b,c an analogy is the same truth.

Flower Drum Pass, example of a failed show:

Start by creating an abstract class of responsibility chain transfer: For example, the following class:

Base class, encapsulates the method that handles handler () and points to the next node Setnexthandler ():

 Public Abstract classHandler {protectedHandler Handler;  PublicHandler Setnexthandler (Handler carhandler) {//points to the next handler object according to the parameter         This. Handler =Carhandler; return  This. Handler; }        //actual operation of the current object     Public Abstract voidhandler ();}

Pass the flower classmate A:

 Public class extends Handler {        @Override    publicvoid  Handler () {        System.out.println (" A performance Programme ");         if (thisnull) {            this. Handler.handler ();     }}}

Pass the flower classmate B:

 Public class extends Handler {        @Override    publicvoid  Handler () {        System.out.println (" B Performance Programme ");         if (thisnull) {            this. Handler.handler ();     }}}

Pass the flower classmate C:

 Public class extends Handler {        @Override    publicvoid  Handler () {        System.out.println (" C Performance Programme ");         if (thisnull) {            this. Handler.handler ();     }}}

Test class:

 Public classTest { Public Static voidMain (string[] args) {Handler Ahandler=NewAhandler (); Handler Bhandler=NewBhandler (); Handler CHandler=NewCHandler (); System.out.println ("-------a==>b==>c--------");        Ahandler.setnexthandler (Bhandler);        Bhandler.setnexthandler (CHandler);                        Ahandler.handler (); System.out.println ("1-------b==>a==>c--------");        Bhandler.setnexthandler (Ahandler);        Ahandler.setnexthandler (CHandler);                Bhandler.handler (); System.out.println ("2-------b==>a==>c--------");        Bhandler.setnexthandler (Ahandler). Setnexthandler (CHandler);    Bhandler.handler (); }    }

Test results:

-------a==>b==>c--------a performance Programme B Performance Show 1-------b==>a==>c--------b Show show a show show C 2 -------B==>a==>c--------b Show show a show C performance show

Design pattern--The responsibility chain mode or the duty chain mode (Chain of Responsibility (CoR))

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.