Pattern: Strategy

Description

Strategy pattern allows a client to choose from a family of algorithms at runtime. It is used when the client expects to have multiple algorithms and wants to choose one of them at runtime.

Example in .NET :

Strategy

using System;
using System.Collections.Generic;

namespace Strategy;
internal class StrategyDemo
{
    public static void SortWithDifferentStrategies()
    {
        List<int> al = new ();
        al.Add(102);
        al.Add(201);
        //sort ascending
        al.Sort((x, y) =&gt; x.CompareTo(y));

        for (int i = 0; i &lt; al.Count; i++)
        {
            Console.WriteLine(al[i]);
        }

        Console.WriteLine("---------------");

        //sort descending
        al.Sort((y, x) =&gt; x.CompareTo(y));
        for (int i = 0; i &lt; al.Count; i++)
        {
            Console.WriteLine(al[i]);
        }
        Console.WriteLine("---------------");
        //sort custom
        al.Sort((x, y) =&gt; LastDigit(x).CompareTo(LastDigit(y)));
        for (int i = 0; i &lt; al.Count; i++)
        {
            Console.WriteLine(al[i]);
        }

        var array = al.FindAll(it =&gt; it &gt; 10);


    }

    static int LastDigit(int x)
    {
        return x % 10;
    }

}


Learn More

Wikipedia

Homework

Image you want to serialize classes to XML,JSON and CSV . Implement a strategy that will allow you to choose between XML , JSON and CSV serialization at runtime.