The model of responsibility chain in Java design pattern

Source: Internet
Author: User

First, what is the responsibility chain model?

The chain of responsibility mode refers to multiple objects, which can sequentially process the information passed in and pass it on to the next object until the processing is complete.

Ii. What are the roles of the chain of responsibility?

    1. Abstract processor role: Defines an interface for processing requests, and, if necessary, defines a method for setting and returning the next processor
    2. Specific processor role: When a request is received by a specific processor, you can choose to dispose of the request or pass the request to the next processor

Third, the code detailed:

Abstract class:  

 Public Abstract classHandler {/*** Next Processor*/    PrivateHandler Target;  PublicHandler Gettarget () {returnTarget; }     Public voidsettarget (Handler target) { This. target =Target; }         Public Abstract voidprocess ();}

Specific implementation of a:

 Public class extends Handler {    @Override    publicvoid  process () {        = gettarget ();         ifnull) {            System.out.println ("A processing, directly returns the result");             return ;         Else {            System.out.println ("A processing, return to the next handler continue processing");            Handler.process ();     }}}

Concrete Implementation B:

 Public class extends handler{    @Override    publicvoid  process () {        = gettarget ();         ifnull) {            System.out.println ("b after processing, directly returning the result")            ; return ;         Else {            System.out.println ("B" after processing, return to the next handler continue processing ");            Handler.process ();     }}}

Client:

 Public class Client {    publicstaticvoid  main (string[] args) {                new handler_a ();         New Handler_b ();        Handlera.settarget (Handlerb);                Handlera.process ();            }    }

Results Print:

After a processing, returns the next handler continues processing B processing, returns the result directly

  

The model of responsibility chain in Java design pattern

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.