Responsibility chain Mode: 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 together and pass the request along the chain until an object handles it.
The idea is simple, for example, to consider employees asking for a raise. The company's managers have a total of three, general manager, director, manager, if an employee asks for a raise, should apply to the Supervisor's manager, if the number of raises in the manager's authority, then the manager can directly approve, otherwise the application will be submitted to the Director. The Director also handles the same, and the general manager can handle all requests. This is the typical chain of responsibility pattern, where the processing of the request forms a chain until an object processes the request. Give a UML diagram of this example.
UML Structure diagram:
An example of a chain of responsibility model
* This example is the case of three types of shop assistants handling orders
* If the order amount is less than 1000, the first level salesperson can process the order
* If the order amount is less than 10000, the level two salesperson can process the order
* If the order amount is less than 100000, the level three salesperson can process the order
Using System; /**////<summary>///salesperson interface, all types of salespersons must implement this interface//</summary> interface Isalesman {string Name {set;get;}//Salesperson name void Setnext (Isalesman nextsalesman); Set the next level salesperson void Process (order order); Processing orders}/**////<summary>///Order class///</summary> class Order {private int orderamount; public int Amount {set{this.orderamount = value;} get{return this.orderamount;} }}/**////<summary>///a salesperson///</summary> class Firstsalesman:isalesman {private Isalesman nextsal Esman = null; private string name = String. Empty; Isalesman member Isalesman member}/**////<summary>///two salesperson///</summary> class Secondsalesman:isalesman {p rivate Isalesman nextsalesman = null; private string name = String. Empty; Isalesman member Isalesman member}/**////<summary>//////</summary> class Thirdsalesman:isalesman {PR Ivate Isalesman nextsalesman = null; private string name = String. EmptY Isalesman member Isalesman member} class Client {public static void Main (string[] args) {Firstsalesman first = new Fir Stsalesman (); First. Name = "Firstman"; Secondsalesman second = new Secondsalesman (); Second. Name = "Secondman"; Thirdsalesman third = new Thirdsalesman (); Third. Name = "Thirdman"; First. Setnext (second); Second. Setnext (third); Order o = New Order (); O.amount = 300; First. Process (o); o = new Order (); O.amount = 1300; First. Process (o); o = new Order (); O.amount = 11300; First. Process (o); Console.read (); } }
Responsibility chain model applicable scenario
There are multiple objects that can handle a request and which object handles that request automatically when the run time is determined.
You want to submit a request to one of multiple objects without explicitly specifying the recipient.
A collection of objects that can handle a request should be specified dynamically.