Responsibility Chain Mode in Design Mode

Source: Internet
Author: User
Tags switch case

The design mode is the abstraction of the solutions to the problems encountered in actual programming, such as predecessors and Daniel. That is, each design mode is embodied in the programming instance. 1. the small embodiment of the responsibility chain mode in programming, let's look at the following situation: (1) if else if .... copy code 1 if (a <12) {2... 3} 4 else if (a <25) {5... 6} 7 else if (a <67) {8... 9} 10 else {11... 12} copy code (2) switch case copy code 1 switch (a) 2 {3 case 1:; break; 4 case 2:; break; 5 case 3:; break; 6 default: 7} copy the code (3) linked list next 1 typedef struct node2 {3 int value; 4 struct node * next; 5} TreeNode; in the three cases above, the common feature is that each branch must be scanned in sequence for judgment and the corresponding branch must be found for processing. (Switches in C ++ and Java also need to traverse each branch) 2. The definition of the responsibility chain mode is an object behavior mode. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain. Requests are transmitted on this chain until an object on the chain decides to process this request. The focus of the responsibility chain model is on the "chain". There is a chain to process similar requests. In the chain, it determines who will process the request and return the corresponding results. In the three examples in 1, the first if, switch, and Tree root node is equivalent to the "link entry". When every branch needs to perform complex business logic processing, it is better to use the responsibility chain mode. 3. The responsibility chain mode is simple. The instance design mode is language-free. Although Java provides keywords corresponding to the specific mechanism, such as clone (prototype mode) and Observable (Observer mode. However, other languages can also implement these functions, but the mechanism may be different. Responsibility Chain Mode C ++ instance Code: (1) the Handler class is the parent class of other specific processing classes A, B, and D, providing abstract and public code. (2) The Code establishes A-> B-> D. (The End Of The responsibility chain must be the default processing class, that is, the default part in the switch.) copy the Code 1 # include <iostream> 2 using namespace std; 3 class Handler {// processing class abstraction 4 public: 5 Handler (int _ value): value (_ value) {}// you can set the number 6 virtual ~ Handler () {} 7 void setNextHandler (Handler * _ nextHandler) {// set the next Handler 8 this-> nextHandler = _ nextHandler; 9} 10 virtual void putHandlerMesg (int _ value) = 0; // The Handler implements the specific processing process 11 12 int value; 13 Handler * nextHandler; 14 }; 15 16 class A: public Handler {// processing class A17 public: 18 A (int _ value): Handler (_ value) {}; 19 ~ A () {}; 20 void putHandlerMesg (int _ value) {21 cout <"A handler this command:" <_ value <endl; 22} 23 }; 24 25 class B: public Handler {// processing class A26 public: 27 B (int _ value): Handler (_ value) {}; 28 ~ B () {}; 29 void putHandlerMesg (int _ value) {30 cout <"B handler this command:" <_ value <endl; 31} 32 }; 33 34 class D: public Handler {// The default processing class D is located at the end of the chain. When other processors cannot process it, use 35 public: 36 D (int _ value ): handler (_ value) {}; 37 void putHandlerMesg (int _ value) {38 cout <"default handler:" <_ value <endl; 39} 40 ~ D () {}; 41}; 42 43 int MainHandle (int handlerValue, Handler * a) {// handle the established responsibility chain 44 Handler * tmp =; 45 while (1) {46 if (tmp-> value = handlerValue) {47 tmp-> putHandlerMesg (handlerValue); 48 break; 49} 50 else if (tmp-> value = 0) 51 {52 tmp-> putHandlerMesg (handlerValue); 53 break; 54} 55 else {56 tmp = tmp-> nextHandler; 57} 58} 59} 60 61 int main () {62 // create a processing chain, a (1) -> B (2)-> d (0, default) 63 Handler * a = new A (1); 64 Ha Ndler * B = new B (2); 65 Handler * d = new D (0); 66 a-> setNextHandler (B); 67 B-> setNextHandler (d ); 68 69 // two examples of processing 70 MainHandle (2, a); 71 MainHandle (4, a); 72 delete (a); 73 delete (B ); 74 delete (d); 75} copy code 4. advantages and disadvantages of the responsible Chain Mode 4.1 the advantages of the responsible Chain Mode enable the code separation between the requester and the handler: the client sending the request does not know which object on the chain finally processes the request, this allows the system to dynamically reorganize and assign responsibilities without affecting the client. Improve system flexibility and scalability. 4.2 The disadvantages of the responsible chain model start with the chain header each time: this is also the disadvantage of the linked list. You may think of a seemingly good solution, such as using hash ing to associate the Request id to be processed with the processing class object, but the system loses scalability.

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.