In this post, we will talk about Strategy design pattern in dotnet core with a concrete example.The source code of this pos is here from my github account(https://github.com/Defcoq/StrategyPatternInDotnetCore).
There are sometimes when we as developer need to change and algorithm at runtime, this is where Strategy design pattern come in mine. In essence, The Strategy pattern is the process of disassociating an algorithm from its host and enabling the ability to swap algorithms dynamically at run time. The Strategy pattern encapsulates algorithms as objects. Clients reference them by an abstract or interface, which enables them to be interchangeable.

. The Context defers all calculations to a ConcreteStrategy referenced by its abstract class or interface (Strategy); the Context may also expose some form of method or property so that the Strategy implementation can be changed.
. The Strategy is an interface for the algorithm. In this instance it contains a single calculate method.
. The ConcreteStrategy is an implementation of the Strategy.
Fire visual studio and create a new blank solution named “StrategyDesignPattern” and add a .net core c# class library called “StrategyPattern.Model” target .net 5

Rename the default class “ Basket” and add the code bellow

What we are trying to build look like in the following diagram.

We are going to apply a discount strategy to the basket, At the moment there is just a simple property to hold the total cost of the basket.
add an interface in the same project called “ IBasketDiscountStrategy”
The single method takes a basket object. An implementation applies a specific discount and then returns the basket price, including the discount.
The first discount strategy you create enables customers to receive a discount if they meet a certain discount threshold. The thresholds are $10 off basket totals over $100 and $5 off basket totals over $50; if the basket value is $50 or below, no discount is applied. Add a new class named BasketDiscountMoneyOff to the project with the following code definition:
The second discount strategy you apply is a percentage off a basket’s total value. When this discount is applied, customers receive 15 percent off the total value of the basket. Add another new class that implements the IBasketDiscountStrategy named BasketDiscountPercentageOff, as shown in the code that follows:
Finally, you need to add a special case discount strategy to be used if no discounts are set. This is an implementation of the Null Object pattern . Add a new class to the project named NoBasketDiscount that is shown here:
This discount strategy simply returns the total cost of the basket without applying any kind of discount algorithm.
To determine which strategy algorithm to apply to a basket, you will use the Factory Method pattern. To enable the Factory Method pattern to build the correct discount strategy, you need to supply it with some information in the form of an enumeration. Create a new class named DiscountType and add it to the project as shown here:
Now with the enum in place, Create the Factory class. Add a new class to the project named BasketDiscountFactory that contains a single static method to create the implementation of the IBasketDiscountStrategy based on the given enumeration as laid out here:
Finally, you can return to the Basket class and update it to include a new constructor and method to return the basket to total cost with an applied discount as shown in the following code:
The basket is completely unaware of the underlying algorithm that will be used to determine the total price, due to the discount strategy being injected to it and referred to using an interface.
The complete project will look like this:

The complete source code can be found here to my github account(https://github.com/Defcoq/StrategyPatternInDotnetCore)
Happy Coding and see you to the next pattern
LarrySig Guest
24.05.2024, 18:18
Post: Comment fonctionnent Internement les guichets automatiques ?
Orvilledop Guest
22.05.2024, 00:57
Post: Comment fonctionnent Internement les guichets automatiques ?
Orvilledop Guest
15.05.2024, 16:04
Post: Comment fonctionnent Internement les guichets automatiques ?
Franck Guest
14.05.2024, 19:34
Post: Comment deployer un site web statique sur github?