Policy mode (stragety pattern)
The strategy pattern is a behavioral pattern, which defines a series of algorithms, encapsulates each algorithm, and allows them to replace each other, allowing the algorithm to change independently of the customer using it.
Using the policy model, you can separate behavior from the environment. The Environment class is responsible for maintaining and querying the behavior classes, and the various algorithms are provided in the specific policy classes.
Role:
1. Abstract strategy (strategy)
This is an abstract role, usually implemented by an interface or abstract class. This role gives the interface required for all the specific policy classes;
2. Specific strategies (concrete strategy)
Implement the specific strategy of the abstract strategy class, packaging the relevant algorithm or behavior;
3. Environment Class (context)
Holds a reference to a strategy class and can select the appropriate policy for the instance according to the logic.
Example:
The namespace Stragetypattern contains the policy base class tax and its 8 implementation classes, and the context environment class holds the policy base class. This example calculates personal income tax in an elegant way.
C # Development Note 04-How to use C # Elegant calculation of 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 the individual income taxes, taxrate is the tax rate, Quickdeduction is the calculator deduction number, calculate calculates the corresponding income the personal income tax.
public class Level0:tax {public Level0 () { taxrate = 0.00m; quickdeduction = 0; }}
Level 0 personal income tax ladder, which represents the initial state of individual 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 = the "; }}
Level 2 personal income tax ladder.
public class Level3:tax {public Level3 () { taxrate = 0.20m; Quickdeduction = 555; }}
Level 3 personal income tax ladder.
public class Level4:tax {public Level4 () { taxrate = 0.25m; Quickdeduction = 1005; }}
Level 4 personal income tax ladder.
public class Level5:tax {public Level5 () { taxrate = 0.30m; Quickdeduction = 2755; }}
Level 5 personal income tax ladder.
public class Level6:tax {public Level6 () { taxrate = 0.35m; Quickdeduction = 5505; }}
Level 6 personal income tax ladder.
public class Level7:tax {public Level7 () { taxrate = 0.45m; Quickdeduction = 13505; }}
Level 7 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, 3 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 <= _tax Level[i]) {_tax = (tax) activator.createinstance (_levels[i]); } } Console.WriteLine ($ "Income = {Income}," + $ "tax = {_tax. Calculate (Result)}! "); return this; }}
Environment class context, the first need to maintain a reference to tax, Exemption_value said the exemption amount (this example uses 3500 yuan), then through reflection and some techniques to select the corresponding tax implementation class to calculate the corresponding ladder of personal income taxes.
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 is specially processed to support the method chain. The following is the output 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 hierarchy structure of the policy class defines an algorithm or a behavior family, the proper use of inheritance can move the common code into the parent class, thus avoiding duplicate code;
2, inheritance can handle a variety of algorithms or behavior, you can avoid the use of multiple conditional transfer statements.
Disadvantages:
1, the client must know all the policy classes, and decide which policy class to use;
2, the strategy mode caused a lot of policy classes, resulting in "sub-class explosion."
Usage scenarios:
1, if there are many classes in a system, the difference between them is only their behavior, then the use of the policy model can dynamically let an object in many behaviors to choose a behavior;
2, a system needs to dynamically select one of several algorithms.
Related articles:
Writing PHP Extension in C + +
C # Tutorial C # data type
Related videos:
What is design mode-php advanced design mode video tutorial