A detailed description of the application of responsibility chain model in C + + design pattern programming

Source: Internet
Author: User
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.

Related Article

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.