Design Model (19) --- responsibility chain model, design model 19 --- responsibilities
I. Definition
Responsibility Chain Mode: Enables multiple objects to process requests to avoid coupling between request senders and recipients. Connect the object to a chain and pass the request along the chain until an object processes it.
Explanation: in simple terms, it should be for a request. Different people process the request according to their own responsibilities. It is also because the request is generally transferred from the bottom layer to the top layer, therefore, different objects that process the request are compared to a chain, which is passed from a relatively "low" place to a relatively "high" place to complete the request operation.
Ii. UML class diagram and basic code
Basic code:
Abstract class Handler {protected Handler successor; public void SetSuccessor (Handler successor) {this. successor = successor;} public abstract void HandleRequest (int request);} class ConcreteHandler1: Handler {public override void HandleRequest (int request) {if (request> = 0 & request <10) {Console. writeLine ("{0} Processing request {1}", this. getType (). name, request);} else if (successor! = Null) {successor. handleRequest (request) ;}} class ConcreteHandler2: Handler {public override void HandleRequest (int request) {if (request >=10 & request <20) {Console. writeLine ("{0} Processing request {1}", this. getType (). name, request);} else if (successor! = Null) {successor. handleRequest (request) ;}} class ConcreteHandler3: Handler {public override void HandleRequest (int request) {if (request >=20 & request <30) {Console. writeLine ("{0} Processing request {1}", this. getType (). name, request);} else if (successor! = Null) {successor. HandleRequest (request );}}}
Client call and result:
Handler h1 = new ConcreteHandler1 (); Handler h2 = new ConcreteHandler2 (); Handler h3 = new ConcreteHandler3 (); h1.SetSuccessor (h2); h2.SetSuccessor (h3 ); int [] requests = {6, 5, 14, 23, 18, 9, 17, 6, 26}; foreach (int request in requests) {h1.HandleRequest (request );}View Code
Iii. Specific instances
List an instance that can better describe the responsibility chain. John asks for leave from the lead in the organization. If the value is less than or equal to 2 days, the manager has the right to approve the case. If the value is less than or equal to 5 days, the manager has the right to approve the case. Otherwise, the manager must submit the case to the General Manager for approval. The Code is as follows:
Class Request {public string RequestType {get; set;} public string RequestContent {get; set;} public int Number {get; set ;}} abstract class Manager {protected string name; protected Manager superior; public Manager (string name) {this. name = name;} public void SetSuperior (Manager superior) {this. superior = superior;} public abstract void RequestApplications (Request request);} class CommonManager: Manager {public CommonManager (string name): base (name) {} public override void RequestApplications (Request request) {if (request. requestType = "leave" & request. number <= 2) {Console. writeLine ("{0 }:{ 1} number is {2} OK", name, request. requestContent, request. number);} else {if (superior! = Null) {superior. requestApplications (request) ;}}} class Majordome: Manager {public Majordome (string name): base (name) {} public override void RequestApplications (Request request) {if (request. requestType = "leave" & request. number <= 5) {Console. writeLine ("{0 }:{ 1} number is {2} OK", name, request. requestContent, request. number);} else {if (superior! = Null) {superior. requestApplications (request) ;}}} class GeneralManager: Manager {public GeneralManager (string name): base (name) {} public override void RequestApplications (Request request) {if (request. requestType = "leave") {Console. writeLine ("{0 }:{ 1} number is {2} OK", name, request. requestContent, request. number);} else if (request. requestType = "add money" & request. number <= 500) {Console. writeLine ("{0 }:{ 1} number is {2} OK", name, request. requestContent, request. number);} else if (request. requestType = "add money" & request. number> 500) {Console. writeLine ("{0 }:{ 1} number is {2} not OK", name, request. requestContent, request. number );}}}View Code
Client call and result:
CommonManager jinli = new CommonManager ("manager"); Majordome zongjian = new Majordome ("Director"); GeneralManager zhongjingli = new GeneralManager ("General Manager"); jinli. setSuperior (zongjian); zongjian. setSuperior (zhongjingli); Request request = new Request (); request. requestType = "leave"; request. requestContent = "john ask for days"; request. number = 2; jinli. requestApplications (request); Request request2 = new Request (); request2.RequestType = "leave"; request2.RequestContent = "john ask for days"; request2.Number = 5; jinli. requestApplications (request2 );View Code
Iv. Advantages and Disadvantages and applicable scenarios
Advantages:
1) reduces the coupling between request senders and handlers.
2) Distribute multiple condition judgments to various processing classes to make the code clearer and the responsibility clearer.
Disadvantages:
1) before finding the correct processing object, all the condition determination should be performed. When the responsibility chain is too long, it may cause performance problems.
2) A request may not be processed.
Applicable scenarios:
1) A system request must be approved by multiple objects.
2) When the Code contains multiple if-else, you can also use the responsibility chain mode to refactor the code.