23 design modes (11): responsible connection mode

Source: Internet
Author: User

Definition:So that multiple objects have the opportunity to process the request, thus avoiding the coupling relationship between the request sender and the receiver. Connect these objects into a chain and pass the request along the chain until an object processes it.

Type:Behavior mode

Class diagram:

First, let's 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 as follows. The method has two parameters: integer I and a request. The value of I determines who processes the request. If I = 1, handler1 is used for processing. If I = 2, handler2 is used for processing, and so on. In programming, this kind of business processing method is very common. All request processing classes include if... Else... The condition judgment statement is linked into a chain of responsibility to process the request. I believe it is often used by everyone. This method has the advantages of being intuitive, simple, and easy to maintain. However, this method also has several headaches:

  • Code bloated:In actual application, the condition is usually not so simple as determining whether the value is 1 or 2. complicated computing may be required, and database query may be required. This requires a lot of additional code, if there are more judgment conditions, then this if... Else... The statement is basically not readable.
  • High Coupling Degree:If we want to add a request processing class, we need to add the else if condition. In addition, the order of the condition determination is also written to death. If we want to change the order, you can only modify this conditional statement.

Now that we know the disadvantages, we have to find a solution. The business logic in this scenario is very simple: if condition 1 is met, handler1 is used for processing, and if condition 2 is not met, it is passed down. If condition 2 is met, handler2 is used for processing, if the condition is not met, continue to pass down, and so on until the condition ends. In fact, the improvement method is also very simple, that is, to put the part of the judgment condition into the processing class, this is the principle of the responsibility connection mode.

 

Structure of the responsible connection mode

The class diagram of the responsible connection mode is very simple. It consists of an abstract class and a group of its implementation classes:

  • Abstract processing class:The abstract processing class mainly contains a member variable nexthandler pointing to the next processing class and a request processing method handrequest. The main idea of the handrequest method is that if the processing conditions are met, this processing class is available for processing; otherwise, nexthandler will process the processing.
  • Specific processing class:The specific processing class mainly implements the specific processing logic and the applicable conditions for processing.

After understanding the general idea of the responsible connection mode, you can better 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; public request (Level level) {This. level = level;} public level getlevel () {return level;} class response {} abstract class handler {private handler nexthandler; public final response handlerequest (Request re Quest) {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 ("----- No suitable processor -----") ;}} return response;} public void setnexthandler (handler) {This. nexthandler = handler;} protected abstract level gethandlerlevel (); public abstract response (request);} class concretehandler1 extends handler {protected level gethandlerlevel () {return new level (1);} public response (request) {system. out. println ("----- requests are processed by processor 1 -----"); return NULL ;}} class concretehandler2 extends handler {protected level gethandlerlevel () {return new level (3 );} public Response response (request) {system. out. println ("----- requests are processed by Processor 2 -----"); return NULL ;}} class concretehandler3 extends handler {protected level gethandlerlevel () {return new level (5 );} public Response response (request) {system. out. println ("----- requests are 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); handler3 ); response response = handler1.handlerequest (new request (new level (4 )));}}

In the code, the level class simulates the judgment conditions; request and response correspond to the request and response respectively; in the abstract class handler, it mainly judges the conditions. Here, it simulates a processing level, the processing class can be processed only when the processing class level is higher than the request level. Otherwise, the processing class is handed over to the next processor for processing. Set the pre-and post-execution relationship of the chain in the client class, and send the request to the first processing class during execution. This is the responsible connection mode. Its functions are similar to the if... Else... The statement is the same.

 

Advantages and disadvantages of the responsibility chain model

Responsibility chain mode and if... Else... The coupling is lower than that of other processing classes because it disperses condition determination into various processing classes, and the priority processing sequence of these processing classes can be set at will. The responsibility chain model also has shortcomings, which are different from if... Else... The statement has the same disadvantage, that is, before finding the correct processing class, all the judgment conditions should be executed again. When the responsibility chain is long, the performance problem is serious.

 

Applicable scenarios of the responsibility chain model

As in the first example, if you use if... Else... Statement to organize a chain of responsibility, when the code looks bad, you can use the chain of responsibility mode for restructuring.

 

Summary

The responsibility chain model is actually a flexible version of if... Else... Statement, which puts the statements of these conditions into various processing classes. This method is flexible, but it also brings risks, for example, when setting the process class prefix/suffix relationship, be sure to carefully check the condition judgment relationship of the processing class prefix/suffix logic, and be careful not to issue circular references in the chain.

From: http://blog.csdn.net/zhengzhb/article/details/7568676

 

Definition:So that multiple objects have the opportunity to process the request, thus avoiding the coupling relationship between the request sender and the receiver. Connect these objects into a chain and pass the request along the chain until an object processes it.

Type:Behavior mode

Class diagram:

First, let's 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 as follows. The method has two parameters: integer I and a request. The value of I determines who processes the request. If I = 1, handler1 is used for processing. If I = 2, handler2 is used for processing, and so on. In programming, this kind of business processing method is very common. All request processing classes include if... Else... The condition judgment statement is linked into a chain of responsibility to process the request. I believe it is often used by everyone. This method has the advantages of being intuitive, simple, and easy to maintain. However, this method also has several headaches:

  • Code bloated:In actual application, the condition is usually not so simple as determining whether the value is 1 or 2. complicated computing may be required, and database query may be required. This requires a lot of additional code, if there are more judgment conditions, then this if... Else... The statement is basically not readable.
  • High Coupling Degree:If we want to add a request processing class, we need to add the else if condition. In addition, the order of the condition determination is also written to death. If we want to change the order, you can only modify this conditional statement.

Now that we know the disadvantages, we have to find a solution. The business logic in this scenario is very simple: if condition 1 is met, handler1 is used for processing, and if condition 2 is not met, it is passed down. If condition 2 is met, handler2 is used for processing, if the condition is not met, continue to pass down, and so on until the condition ends. In fact, the improvement method is also very simple, that is, to put the part of the judgment condition into the processing class, this is the principle of the responsibility connection mode.

 

Structure of the responsible connection mode

The class diagram of the responsible connection mode is very simple. It consists of an abstract class and a group of its implementation classes:

  • Abstract processing class:The abstract processing class mainly contains a member variable nexthandler pointing to the next processing class and a request processing method handrequest. The main idea of the handrequest method is that if the processing conditions are met, this processing class is available for processing; otherwise, nexthandler will process the processing.
  • Specific processing class:The specific processing class mainly implements the specific processing logic and the applicable conditions for processing.

After understanding the general idea of the responsible connection mode, you can better 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; public request (Level level) {This. level = level;} public level getlevel () {return level;} class response {} abstract class handler {private handler nexthandler; public final response handlerequest (Request re Quest) {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 ("----- No suitable processor -----") ;}} return response;} public void setnexthandler (handler) {This. nexthandler = handler;} protected abstract level gethandlerlevel (); public abstract response (request);} class concretehandler1 extends handler {protected level gethandlerlevel () {return new level (1);} public response (request) {system. out. println ("----- requests are processed by processor 1 -----"); return NULL ;}} class concretehandler2 extends handler {protected level gethandlerlevel () {return new level (3 );} public Response response (request) {system. out. println ("----- requests are processed by Processor 2 -----"); return NULL ;}} class concretehandler3 extends handler {protected level gethandlerlevel () {return new level (5 );} public Response response (request) {system. out. println ("----- requests are 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); handler3 ); response response = handler1.handlerequest (new request (new level (4 )));}}

In the code, the level class simulates the judgment conditions; request and response correspond to the request and response respectively; in the abstract class handler, it mainly judges the conditions. Here, it simulates a processing level, the processing class can be processed only when the processing class level is higher than the request level. Otherwise, the processing class is handed over to the next processor for processing. Set the pre-and post-execution relationship of the chain in the client class, and send the request to the first processing class during execution. This is the responsible connection mode. Its functions are similar to the if... Else... The statement is the same.

 

Advantages and disadvantages of the responsibility chain model

Responsibility chain mode and if... Else... The coupling is lower than that of other processing classes because it disperses condition determination into various processing classes, and the priority processing sequence of these processing classes can be set at will. The responsibility chain model also has shortcomings, which are different from if... Else... The statement has the same disadvantage, that is, before finding the correct processing class, all the judgment conditions should be executed again. When the responsibility chain is long, the performance problem is serious.

 

Applicable scenarios of the responsibility chain model

As in the first example, if you use if... Else... Statement to organize a chain of responsibility, when the code looks bad, you can use the chain of responsibility mode for restructuring.

 

Summary

The responsibility chain model is actually a flexible version of if... Else... Statement, which puts the statements of these conditions into various processing classes. This method is flexible, but it also brings risks, for example, when setting the process class prefix/suffix relationship, be sure to carefully check the condition judgment relationship of the processing class prefix/suffix logic, and be careful not to issue circular references in the chain.

From: http://blog.csdn.net/zhengzhb/article/details/7568676

 

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.