[Design mode] -- responsibility chain mode: chainofresponsibility

Source: Internet
Author: User

[Mode overview] ---------- by xingoo

Pattern intent

Avoid excessive coupling between request senders and recipients. A requester only needs to send a request. The request is responded to by a specific object, and does not need to be concerned. The request recipient can process it by himself, or pass it back like a chain.

Therefore, requests may be lost, or there is no definite security guarantee.

Application scenarios

1. Reduce Coupling

2. enhancing the flexibility of assigning duties

3 not guaranteed to be accepted

Mode Structure

  HandlerDefine an interface to implement the backward transmission process

 1 abstract class Handler{ 2     protected Handler successor; 3      4     public abstract void handleRequest(); 5      6     public Handler getSuccessor() { 7         return successor; 8     } 9     10     public void setSuccesor(Handler successor) {11         this.successor = successor;12     }13     14 }

 

  ConcretehandlerIt can be responsible for the request, or it can be passed back.

 1 class ConcreteHandler extends Handler{ 2     public void handleRequest(){ 3         if(getSuccessor() != null){ 4             System.out.println("getSuccessor !"); 5             getSuccessor().handleRequest(); 6         }else{ 7             System.out.println("handle in this! request()!"); 8         } 9     }10 }

 

All code

 1 package com.xingoo; 2 abstract class Handler{ 3     protected Handler successor; 4      5     public abstract void handleRequest(); 6      7     public Handler getSuccessor() { 8         return successor; 9     }10     11     public void setSuccesor(Handler successor) {12         this.successor = successor;13     }14     15 }16 class ConcreteHandler extends Handler{17     public void handleRequest(){18         if(getSuccessor() != null){19             System.out.println("getSuccessor !");20             getSuccessor().handleRequest();21         }else{22             System.out.println("handle in this! request()!");23         }24     }25 }26 public class Client {27     public static void main(String[] args) {28         Handler handle1,handle2,handle3;29         handle1 = new ConcreteHandler();30         handle2 = new ConcreteHandler();31         handle3 = new ConcreteHandler();32         handle1.setSuccesor(handle2);33         handle2.setSuccesor(handle3);34         handle1.handleRequest();35     }36 }
View code

Running result

getSuccessor !getSuccessor !handle in this! request()!

 

[Design mode] -- responsibility chain mode: chainofresponsibility

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.