Home  >  Article  >  Backend Development  >  In-depth understanding of the strategy pattern of C# design patterns and role-specific case sharing

In-depth understanding of the strategy pattern of C# design patterns and role-specific case sharing

php是最好的语言
php是最好的语言Original
2018-07-28 11:57:552866browse

Strategy Pattern

The strategy pattern is a behavioral pattern. It defines a series of algorithms, encapsulates each algorithm, and makes them Can be interchanged, allowing the algorithm to change independently of the client using it.

Using the strategy pattern can separate behavior and environment. The environment class is responsible for maintaining and querying the behavior class, and various algorithms are provided in the specific strategy class.

Role:

1. Abstract strategy (Strategy)

This is an abstract role, usually implemented by an interface or abstract class. This role gives all the interfaces required by concrete strategy classes;

2. Concrete Strategy

A concrete strategy class that implements an abstract strategy and packages related algorithms or behaviors;

3. Environment class (Context)

holds a reference to the Strategy class and can select the corresponding strategy for the instance based on logic.

Example:

In-depth understanding of the strategy pattern of C# design patterns and role-specific case sharing

The namespace StrategyPattern contains the strategy base class Tax and its 8 implementation classes, and the Context environment class holds There are strategy base classes. This example provides an elegant way to calculate personal income tax.

C# Development Notes 04-How to use C# to elegantly calculate personal income tax?

namespace StragetyPattern
public abstract class Tax {

    protected decimal TaxRate = 0;

    protected decimal QuickDeduction = 0;

    public virtual decimal Calculate(decimal income) {
        return income * TaxRate - QuickDeduction;
    }

}

The strategy base class Tax represents personal income tax, TaxRate is the tax rate, QuickDeduction is the quick calculation deduction, and Calculate calculates the personal income tax of the corresponding income.

public class Level0 : Tax {

    public Level0() {
        TaxRate = 0.00m;
        QuickDeduction = 0;
    }

}

Level 0 personal income tax ladder represents the initial status of personal income tax.

public class Level1 : Tax {

    public Level1() {
        TaxRate = 0.03m;
        QuickDeduction = 0;
    }

}

Level 1 personal income tax ladder.

public class Level2 : Tax {

    public Level2() {
        TaxRate = 0.10m;
        QuickDeduction = 105;
    }

}

Level 2 personal income tax ladder.

public class Level3 : Tax {

    public Level3() {
        TaxRate = 0.20m;
        QuickDeduction = 555;
    }

}

3 levels of personal income tax ladder.

public class Level4 : Tax {

    public Level4() {
        TaxRate = 0.25m;
        QuickDeduction = 1005;
    }

}

4 levels of personal income tax ladder.

public class Level5 : Tax {

    public Level5() {
        TaxRate = 0.30m;
        QuickDeduction = 2755;
    }

}

5-level personal income tax ladder.

public class Level6 : Tax {

    public Level6() {
        TaxRate = 0.35m;
        QuickDeduction = 5505;
    }

}

6-level personal income tax ladder.

public class Level7 : Tax {

    public Level7() {
        TaxRate = 0.45m;
        QuickDeduction = 13505;
    }

}

7 levels of personal income tax ladder.

public class Context {

    private Tax _tax = null;

    private const decimal EXEMPTION_VALUE = 3500m;

    private List<decimal> _taxLevel = new List<decimal>{
        0,
        1500,
        4500,
        9000,
        35000,
        55000,
        80000,
        decimal.MaxValue
    };

    private List<Type> _levels = new List<Type>();

    private void GetLevels() {
        _levels = AppDomain.CurrentDomain.GetAssemblies()
                           .SelectMany(tp => tp.GetTypes()
                           .Where(t => t.BaseType == typeof(Tax)))
                           .ToList();
    }

    public Context() {
        GetLevels();
    }

    public Context Calculate(decimal income) {
        _tax = new Level0();
        var result = income - EXEMPTION_VALUE;
        for(int i = 1; i <= _taxLevel.Count - 1; i++) {
            if(result > _taxLevel[i - 1] && result <= _taxLevel[i]) {
                _tax = (Tax)Activator.CreateInstance(_levels[i]);
            }
        }
        Console.WriteLine($"Income = {income}," + $"tax = {_tax.Calculate(result)}!");
        return this;
    }

}

Environment class Context, first need to maintain a reference to Tax, EXEMPTION_VALUE represents the exemption amount (3500 yuan is used in this example), and then select the corresponding Tax implementation class through reflection and some techniques to calculate the individuals of the corresponding ladder Income Tax.

public class Program {

    private static Context _context = new Context();

    public static void Main(string[] args) {
        _context.Calculate(2500.00m)
                .Calculate(4900.00m)
                .Calculate(5500.00m)
                .Calculate(7000.00m)
                .Calculate(10000.00m)
                .Calculate(16000.00m)
                .Calculate(43000.00m)
                .Calculate(70000.00m)
                .Calculate(100000.00m)
                .Calculate(4500.00m)
                .Calculate(1986.00m);

        Console.ReadKey();
    }

}

The above is the caller's code, Calculate has been specially processed to support method chaining. The following is the output result of this case:

Income = 2500.00,tax = 0.0000!
Income = 4900.00,tax = 42.0000!
Income = 5500.00,tax = 95.0000!
Income = 7000.00,tax = 245.0000!
Income = 10000.00,tax = 745.0000!
Income = 16000.00,tax = 2120.0000!
Income = 43000.00,tax = 9095.0000!
Income = 70000.00,tax = 17770.0000!
Income = 100000.00,tax = 29920.0000!
Income = 4500.00,tax = 30.0000!
Income = 1986.00,tax = 0.0000!

Advantages:

1. The hierarchical structure of the strategy class defines an algorithm or behavior family. Proper use of inheritance can convert public Move the code into the parent class to avoid duplication of code;
2. Inheritance can handle a variety of algorithms or behaviors and avoid using multiple conditional transfer statements.

Disadvantages:

1. The client must know all the policy classes and decide which one to use;
2. The strategy mode causes a lot of problems Strategy class, causing "subclass explosion".

Usage scenarios:

1. If there are many classes in a system and the difference between them is only their behavior, then the strategy pattern can be used to dynamically Let an object choose one behavior among many behaviors;
2. A system needs to dynamically choose one of several algorithms.

Related articles:

Writing PHP Extension using C/C

##[c# tutorial]C# data type

Related videos:

What is design pattern-php advanced design pattern video tutorial

The above is the detailed content of In-depth understanding of the strategy pattern of C# design patterns and role-specific case sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn