Usage of the responsibility chain pattern in. Net

Source: Internet
Author: User
The responsibility chain pattern allows multiple objects to have the opportunity to process requests, thereby avoiding coupling between the sender and receiver of the request. Connect the objects together into a chain and pass the request along this chain until one has handled it.

The responsibility chain pattern allows multiple objects to have the opportunity to process requests, thereby avoiding coupling between the sender and receiver of the request. Connect the objects together into a chain and pass the request along this chain until one has handled it.

The results of the responsibility chain pattern are as follows:

.

Handler: Defines an interface for processing requests and implements a successor chain.

Concretehandler: handles the responsible request and can access the successor. If the request can be processed, it is processed, otherwise the request is forwarded to the successor.

Client: Submits a request to a specific processor object on the chain.

Here we first use a simple example to calculate the income tax to illustrate the role of the responsibility chain model in the application

First of all, a brief account of the method of calculating income tax:

Take Chengdu as an example:

Calculated amount----------------------Taxable Amount---------tax rate

1, 2000 yuan/month tax exemption;

2, 2000 yuan/month above to 2500 yuan/month----No more than 500 yuan----5%

3, 2500 yuan/month above to 4000 yuan/month----500 yuan to 2000 yuan----10%

4, 4000 yuan/month above to 7000 yuan/month----2000 yuan to 5000 yuan----15%

5, 7000 yuan/month above to 22000 yuan/month----5000 yuan to 20000 yuan----20%

6, 22000 yuan/month above to 42000 yuan/month----20000 yuan to 40000 yuan----25%

7, 42000 yuan/month above to 62000 yuan/month----40000 yuan to 60000 yuan----30%

8, 62000 yuan/month above to 82000 yuan/month----60000 yuan to 80000 yuan----35%

9, 82000 yuan/month above to 102000 yuan/month----80000 yuan to 100000 yuan----40%

10, 102000 yuan/month above----more than 100000 yuan----45%

(Salary-2000) * A tax rate for the corresponding wage range

When you see a calculation like this, do you have your own thoughts in mind? Write a series of if statements to make a judgment. But this kind of writing can achieve the present demand, But if the tax rate changes you have to come up with your program step-by-step changes. If the number of bars in the IF statement changes, we must also add the necessary if judgments to the code. This is quite cumbersome for the maintenance of the program. If we use the responsibility chain model, it can be quite simple.

Let's talk about our design ideas first.

We can use the responsibility chain to complete the calculation of income tax, which is responsible for calculating the income tax of a certain section. If the calculation condition is met, then the next object is evaluated. Here we write down the core code.

The following are the referenced contents:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Comptax
{
    public class Comptax
    {
   & nbsp;    private double Taxp;
        private Double minincome;
        private static double disc=2000;
        private Comptax nextcomptax;
        public Comptax (double minincome, double taxp)
   & nbsp;    {
            this. Minincome = Minincome;
            this. TAXP = TAXP;
            nextcomptax = null;
       }
        public double Comp (double income)
         {

if (Income > Minincome && (nextcomptax==null income< nextcomptax.minincome))
{
Return (INCOME-DISC) * TAXP;
}
Else
{
return Nextcomptax.comp (income);
}
}

public static Comptax getinstance ()
{
Comptax[] CP ={
New Comptax (2000,0.05),
New Comptax (2500,0.1),
New Comptax (4000,0.15),
New Comptax (7000,0.2),
New Comptax (22000,0.25),
New Comptax (42000,0.3),
New Comptax (62000,0.35),
New Comptax (82000,0.4),
New Comptax (102000,0.45)
};
Comptax Ct=null;
for (int i = 0; i < CP. Length-1; i++)
{
if (i = = 0)
ct = cp[i];
Cp[i]. Nextcomptax = cp[i + 1];
}
return CT;
}
}
}



And then call on the page.

The following are the referenced contents:

private void Button1_Click (object sender, EventArgs e)
{
TextBox2.Text = Comptax.getinstance (). Comp (double. Parse (TextBox1.Text)). ToString ();
}


This allows us to connect the various possibilities in the getinstance static method. If the first object is not satisfied, it is processed with the next object. Until it can be handled.

Here we can read the CP array from the configuration file, thus implementing the dynamic configuration. Speaking of which, we still have a lot of places.

This design pattern can be used. For example, when our membership points function, when the points to how much can be done, etc. can also be easily solved with the responsibility chain.



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.