Responsibility chain mode instance (C #)

Source: Internet
Author: User

The following is a specific example of the use of the accused chain mode, you can easily understand the accused chain mode.

In fact, the accusation chain is simply to define a accusation process. Like a link, each node has its own processing scope. If it cannot be processed, to the next node of your reference.

 

The following example is also a simple process. Each budget needs to be reviewed by different persons. Different persons have different audit scopes. if the funds are within the scope of their processing, they can be processed directly. if the funds are not within the scope of their own, it is passed into your superiors for processing. When writing an instance, You need to define the processing process. It defines the processing link.

 

The source code is as follows:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Chain_of_Responsibility_Use{    class PurchaseReuest    {        private int number;        private double amount;        private string purpose;        public PurchaseReuest(int number, double amount, string purpose)        {            this.amount = amount;            this.number = number;            this.purpose = purpose;        }        public int Number        {            get { return number; }            set { number = value; }        }        public double Amount        {            get { return amount; }            set { amount = value; }        }        public string Purpose        {            get { return purpose; }            set { purpose = value; }        }    }    abstract class Approver    {        protected string name;        protected Approver successor;        public Approver(string name)        {            this.name = name;        }        public void setSuccessor(Approver successor)        {            this.successor = successor;        }        abstract public void ProcessRequest(PurchaseReuest request);    }    //ConcreteHandler    class Director : Approver    {        public Director(string name)            : base(name) { }        public override void ProcessRequest(PurchaseReuest request)        {            //throw new NotImplementedException();            if (request.Amount < 10000.0)                Console.WriteLine("{0} {1} approved request# {2} ", this, name, request.Number);            else                if (successor != null)                    successor.ProcessRequest(request);        }    }    //ConcreteHandler    class VicePresident : Approver    {        public VicePresident(string name) : base(name) { }        public override void ProcessRequest(PurchaseReuest request)        {            //throw new NotImplementedException();            if (request.Amount < 25000.0)                Console.WriteLine("{0} {1} approved request# {2} ", this, name, request.Number);            else                if (successor != null)                    successor.ProcessRequest(request);        }    }    //ConcreteHandler    class President : Approver    {        public President(string name) : base(name) { }        public override void ProcessRequest(PurchaseReuest request)        {            //throw new NotImplementedException();            if (request.Amount < 100000.0)                Console.WriteLine("{0} {1} approved request# {2} ", this, name, request.Number);            else                Console.WriteLine("Request# {0} request " + "an executie meeting ! ", request.Number);        }    }    class Program    {        static void Main(string[] args)        {            Director Larry = new Director("Larry");            VicePresident Sam = new VicePresident("Sam");            President Tammy = new President("Tammy");            Larry.setSuccessor(Sam);            Sam.setSuccessor(Tammy);            PurchaseReuest rs = new PurchaseReuest(2034, 350.00, "Supplies");            Larry.ProcessRequest(rs);            PurchaseReuest rx = new PurchaseReuest(2035, 12590.10, "Project X");            Larry.ProcessRequest(rx);            PurchaseReuest ry = new PurchaseReuest(2036, 122100.00, "Project Y");            Larry.ProcessRequest(ry);            Console.ReadKey();        }    }}

The following is an example class diagram:

 

The following is an example of the relationship diagram:

 

The example is as follows:

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.