responsibility Chain mode (Chain of Responsibility): enables multiple objects to have the opportunity to process requests, avoids coupling between the sender and receiver of the request, links all processing objects to a chain, and passes the request along the chain until an object has processed it.
? Request delivery direction: from low to advanced
? Focus: How to form this chain of responsibility
? Feature: the client issuing the processing request does not know which object in the system is ultimately processing the request, so that changes to the system can be dynamically re-organized and distributed without impacting the Client.
1. Features that are available in derived classes
(1) to set the successor (same)
(2) can achieve their own functions within the scope of the function (approval function, specific approval details are not the Same)
(3) the ability to pass requests that they cannot handle to the parent department (same)
2. different functions
The specific treatment is Different.
3. Examples and Code implementations
Title: It is known that the purchase approval of an enterprise is graded, that is, according to the different levels of the purchasing amount of the competent personnel to approve, the Director can approve the purchase order of less than 50,000 yuan (excluding 50,000 yuan), the vice chairman can approve 50,000 yuan to 100,000 yuan (excluding 100,000 Yuan) purchase order, The chairman can approve 10 Million to 500,000 Yuan (excluding 500,000 Yuan) purchase orders, 500,000 yuan and above the purchase order will need to meet to discuss the Decision. Adopt the responsibility chain design pattern (Chain of Responsibility).
3.1, the request class purchaserequest, the purchase Request.
1 packagecom.designmode.chainofresponsibility;2 3 public classPurchaserequest {4 5 Private DoubleAmount//The amount of a purchase6 Private intNumber//Purchase order number7 PrivateString purpose;//Purpose of Purchase8 9 public Doublegetamount () {Ten returnamount; one } a public voidSetamount (DoubleAmount) { - this. Amount =amount; - } the public intgetnumber () { - returnnumber ; - } - public voidSetnumber (intnumber ) { + this. Number =number ; - } + publicString getpurpose () { a returnpurpose; at } - public voidsetpurpose (String Purpose) { - this. Purpose =purpose; - } - - in}purchaserequest
3.2, the approval of the common parent class approver
1 packagecom.designmode.chainofresponsibility;2 3 public classApprover {4 5 PrivateApprover successor;//Superior6 7 /**8 * Purchase Order Approval9 */Ten public voidProcessRequest (purchaserequest Request) { one if(successor!=NULL) { a successor.processrequest (request); - } - } the - /** - * Set up superior - * @paramsuccessor + */ - public voidsetsuccessor (approver Successor) { + this. successor=successor; a } at}Approver
3.3, the director approval sub-category Director
1 packagecom.designmode.chainofresponsibility;2 3 /**4 * Director's Approval5 */6 public classDirectorextendsApprover {7 8 public voidProcessRequest (purchaserequest Request) {9 if(request.getamount () <50000){TenSystem.out.println ("director has approved"); one}Else { a Super. ProcessRequest (request); - } - } the}Director
3.4, Vice Chairman approval sub-category VicePresident
1 packagecom.designmode.chainofresponsibility;2 3 /**4 * Approval by Vice Chairman5 */6 public classVicePresidentextendsApprover {7 8 public voidProcessRequest (purchaserequest Request) {9 if(request.getamount () >=50000&&request.getamount () <100000){TenSYSTEM.OUT.PRINTLN ("vice chairman approves and Approves")); one}Else { a Super. ProcessRequest (request); - } - } the}vicepresident
3.5, The Chairman approval sub-category President
1 packagecom.designmode.chainofresponsibility;2 3 /**4 * Board Approval5 */6 public classPresidentextendsApprover {7 8 public voidProcessRequest (purchaserequest Request) {9 if(request.getamount () >=100000&&request.getamount () <500000){TenSYSTEM.OUT.PRINTLN ("the chairman approves the approval"); one}Else { a Super. ProcessRequest (request); - } - } the}President
3.6, The meeting approval sub-category Congress
1 packagecom.designmode.chainofresponsibility;2 3 /**4 * Approval of the meeting5 */6 public classCongressextendsApprover {7 8 public voidProcessRequest (purchaserequest Request) {9 if(request.getamount () >=500000){TenSYSTEM.OUT.PRINTLN ("the meeting approves the approval"); one}Else { a Super. ProcessRequest (request); - } - } the}Congress
3.7. Test Class Approvertest
1 packagecom.designmode.chainofresponsibility;2 3 Importjava.util.Scanner;4 5 public classApprovertest {6 7@SuppressWarnings ("resource")8 public Static voidmain (string[] Args) {9Congress Meeting =NewCongress ();TenPresident President =NewPresident (); oneVicePresident vicepresident =Newvicepresident (); aDirector Director =NewDirector (); - - //structuring the chain of responsibility the director.setsuccessor (vicepresident); - vicepresident.setsuccessor (president); - president.setsuccessor (meeting); - +Scanner reader =NewScanner (system.in); -Purchaserequest request =Newpurchaserequest (); + request.setamount (reader.nextint ()); aDirector.processrequest (request);//Start Approval at } -}approvertest
Design pattern of responsibility chain in Java design pattern