Strategy pattern

Strategy pattern

What is the strategy pattern?

Via wikipedia

For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, or other discriminating factors. These factors are not known for each case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication.

Strategy_Pattern_in_UML
Link

When to use it?

When an object / function may behave differently behavior during the operation of our application. Example: We have the prices of our products but these prices change depending on who buys.

Let’s see how this works on a practical example.

Example

Our strategy interface looks like this:

interface ICalculatePrice
{
    float CalculatePrice(float price);
}

Now we will take care of calculate the price for our contractors.

For regular customers we give them a 30% discount.

class LowPrice : ICalculatePrice
{
    public float CalculatePrice(float price)
    {
        return price * 0.7f;
    }
}

Normal customers buy at the base price.

class NormalPrice : ICalculatePrice
{
    public float CalculatePrice(float price)
    {
        return price;
    }
}

And for special customers (eg, those we don’t like 🙂 ) we add to the price 30%.

class OverpricedPrice : ICalculatePrice
{
    public float CalculatePrice(float price)
    {
        return price * 1.3f;
    }
}

Let’s create price list class.

class PriceList
{
    private readonly ICalculatePrice _priceCalculator;
    private readonly float productPrice = 60;

    public PriceList(ICalculatePrice priceCalculator)
    {
        _priceCalculator = priceCalculator;
    }

    public float ShowPriceOfTheProduct()
    {
        return _priceCalculator.CalculatePrice(productPrice);
    }
}

Our class got information what price calculation adopt and set the base price of the product at 60.

In such a simple way we can now set prices for our customers.

class Program
{
    static void Main(string[] args)
    {
        //regular customer
        PriceList priceListForRegularCustomers = new PriceList(new LowPrice());
        Console.WriteLine($"Price for regular customers is: {priceListForRegularCustomers.ShowPriceOfTheProduct()}\n");

        //normal customer
        PriceList priceListForNormalCustomers = new PriceList(new NormalPrice());
        Console.WriteLine($"Price for normal customers is: {priceListForNormalCustomers.ShowPriceOfTheProduct()}\n");

        //special customer
        PriceList priceListForSpecialCustomers = new PriceList(new OverpricedPrice());
        Console.WriteLine($"Price for special customers is: {priceListForSpecialCustomers.ShowPriceOfTheProduct()}\n");

        Console.ReadKey();
    }
}

Strategy pattern

Summary

It is one of the most commonly used behavior patterns and it’s relatively easy to implement.On the blog i also have a description of patters like : composite , state , observer and command. The rest you will find HERE(PL).

Link to the project.

 

Leave a Reply

Your email address will not be published. Required fields are marked *