


In-depth understanding of the strategy pattern of C# design patterns and role-specific case sharing
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:
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?
1 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
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.
1 2 3 4 5 6 7 8 |
|
Level 0 personal income tax ladder represents the initial status of personal income tax.
1 2 3 4 5 6 7 8 |
|
Level 1 personal income tax ladder.
1 2 3 4 5 6 7 8 |
|
Level 2 personal income tax ladder.
1 2 3 4 5 6 7 8 |
|
3 levels of personal income tax ladder.
1 2 3 4 5 6 7 8 |
|
4 levels of personal income tax ladder.
1 2 3 4 5 6 7 8 |
|
5-level personal income tax ladder.
1 2 3 4 5 6 7 8 |
|
6-level personal income tax ladder.
1 2 3 4 5 6 7 8 |
|
7 levels of personal income tax ladder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 |
|
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.
