23 Design Modes (19)-Chain of responsibility mode

Source: Internet
Author: User
Tags abstract continue
23 Design Modes (19)-Chain of responsibility mode

Definition: Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Link the objects to a chain and pass the request along this chain until an object has processed it.


Type: Behavior class mode


Class Diagram:

First look at a piece of code:

public void Test (int i, request request) {
if (i==1) {
Handler1.response (Request);
}else if (i = = 2) {
Handler2.response (Request);
}else if (i = = 3) {
Handler3.response (Request);
}else if (i = = 4) {
Handler4.response (Request);
}else{
Handler5.response (Request);
}
}


The business logic of the code is this, the method has two parameters: the integer i and a request, according to the value of I to determine who will handle the request, if i==1, handled by Handler1, if i==2, handled by Handler2, and so on.


In programming, this method of handling the business is very common, and all the classes that process the request have if...else ... Conditional judgment statements are linked to a chain of responsibility to deal with the request, I believe we are often used. The advantages of this approach are very straightforward, straightforward, and easier to maintain, but there are several more troubling questions about this approach:


Code bloat: The actual application of the decision condition is usually not so simple to determine whether it is 1 or whether it is 2, perhaps the need for complex calculations, perhaps the need to query the database and so on, there will be a lot of extra code, if the judging condition is more, then this if...else ... The statement is basically impossible to read.


High coupling: If we want to continue to add the class that processes the request, then we will continue to add the else if condition, in addition, the order of this condition is also written dead, if you want to change the order, then you can only modify the conditional statement.


Since we already know the shortcomings, we must find a way to solve them. The business logic for this scenario is simple: if the condition 1 is met, it is handled by Handler1, not satisfied then passed down, if the condition 2 is met, then the Handler2 is processed, not satisfied then continues to pass, and so on until the condition ends. In fact, the method of improvement is also very simple, that is, the determination of the conditions of the part of the processing class, which is the principle of the responsibility of the model.


The structure of the responsibility chain model

The class diagram of the responsibility chain pattern is very simple, and consists of an abstract processing class and its set of implementation classes:

Abstract Processing class: An abstract processing class that mainly contains a member variable Nexthandler to the next processing class and a method to process the request the main idea of the Handrequest,handrequest method is that if the condition of the processing is satisfied, there is a processing class to handle it. Otherwise, it will be handled by Nexthandler.

The concrete processing class: The concrete processing class is mainly to the concrete processing logic and the processing application condition carries on the realization.


After understanding the general idea of the responsibility chain model, it is better to understand the code:

Class Level {
private int level = 0;
Public level (Int. level) {
This.level = level;
};

Public Boolean above (level level) {
if (this.level >= level.level) {
return true;
}
return false;
}
}

Class Request {
Level level;
Public Request (level) {
This.level = level;
}

Public level Getlevel () {
return level;
}
}

Class Response {

}

Abstract class Handler {
Private Handler Nexthandler;
Public final Response HandleRequest (Request request) {
Response Response = null;

if (This.gethandlerlevel (). Above (Request.getlevel ())) {
Response = This.response (request);
}else{
if (This.nexthandler! = null) {
This.nextHandler.handleRequest (Request);
}else{
SYSTEM.OUT.PRINTLN ("-----does not have a suitable processor-----");
}
}
return response;
}
public void Setnexthandler (Handler Handler) {
This.nexthandler = handler;
}
Protected abstract level gethandlerlevel ();
Public abstract Response Response (Request request);
}

Class ConcreteHandler1 extends Handler {
Protected level Gethandlerlevel () {
return new level (1);
}
Public Response Response (Request request) {
SYSTEM.OUT.PRINTLN ("-----request is processed by processor 1-----");
return null;
}
}

Class ConcreteHandler2 extends Handler {
Protected level Gethandlerlevel () {
return new level (3);
}
Public Response Response (Request request) {
SYSTEM.OUT.PRINTLN ("-----request is processed by processor 2-----");
return null;
}
}

Class ConcreteHandler3 extends Handler {
Protected level Gethandlerlevel () {
return new level (5);
}
Public Response Response (Request request) {
SYSTEM.OUT.PRINTLN ("-----request is processed by Processor 3-----");
return null;
}
}

public class Client {
public static void Main (string[] args) {
Handler handler1 = new ConcreteHandler1 ();
Handler handler2 = new ConcreteHandler2 ();
Handler Handler3 = new ConcreteHandler3 ();

Handler1.setnexthandler (Handler2);

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.