“That’s simple as door” I wrote in Ukrainian, but I’m quite sure that in English another word combination is used to refer to express simple things. When it is raining outside you wear coat and take umbrella, and when it is sweltering you take T-shirt and sunglasses. But instead of taking a look outside to identify the weather and walk to wardrobe and take clothes and then, keeping in mind weather condition walk to bracket and take sunglasses or umbrella, YOU wake up and your wife (or husband, what is less likely) gives everything you need. In other words your wearing strategy for today has been given to you with one shot.

STRATEGY

Strategy – is design pattern that means having family of algorithms that can be used/changed by your code depending on situation.
Lets imagine that in class Myself we have some method GoOutside()  where we chose clothes and accessories and then walking outside.
Method could possibly look like below:
        public void GoOutside()
        {
            var weather = Weather.GetWeather();
            string clothes = GetClothes(weather);
            string accessories = GetAccessories(weather);
            Console.WriteLine("Today I wore {0} and took {1}", clothes, accessories);
        }

        private string GetAccessories(string weather)
        {
            string accessories;
            switch (weather)
            {
                case "sun":
                    accessories = "sunglasses";
                    break;
                case "rain":
                    accessories = "umbrella";
                    break;
                default:
                    accessories = "nothing";
                    break;
            }
            return accessories;
        }

        private string GetClothes(string weather)
        {
            string clothes;
            switch (weather)
            {
                case "sun":
                    clothes = "T-Shirt";
                    break;
                case "rain":
                    clothes = "Coat";
                    break;
                default:
                    clothes = "Shirt";
                    break;
            }
            return clothes;
        }
It doesn’t offend anyone, but once we will need to adapt to snow shower, we would need add one more case in 300 more places. From one point of view that is not difficult, but from another it “can get you” or maybe code with method GoOuside cannot be changed any longer. What’s then?
Hereabove I showed example with switch statement, since Strategy is the most elegant way to get rid of this monster.
    internal class Myself
    {
        private IWearingStrategy _wearingStrategy = new DefaultWearingStrategy();

        public void ChangeStrategy(IWearingStrategy wearingStrategy)
        {
            _wearingStrategy = wearingStrategy;
        }

        public void GoOutside()
        {
            var clothes = _wearingStrategy.GetClothes();
            var accessories = _wearingStrategy.GetAccessories();

            Console.WriteLine("Today I wore {0} and took {1}", clothes, accessories);
        }
    }
As we can see interface has two methods. They are shown below as well as SunshineWearingStrategy implementation:
    public interface IWearingStrategy
    {
        string GetClothes();
        string GetAccessories();
    }

    class SunshineWearingStrategy : IWearingStrategy
    {
        public string GetClothes()
        {
            return "T-Shirt";
        }

        public string GetAccessories()
        {
            return "sunglasses";
        }
    }

All left is to correctly chose strategy and set it for Myself instance. Hm! Anyway someone have to take a look on weather and put right strategy (wife, who woke up in the morning). But we can do this in one different place.

            var me = new Myself();
            me.ChangeStrategy(new RainWearingStrategy());
            me.GoOutside();

Output: “Today I wore Coat and took umbrella”

Yet another example would be changing sorting algorithm depending on list length. We all know that qsort is not best solution for small collections or superlarge, in one case we would use merge or other sorting and in another case we would use heapsort. Having sorting logic separately and changing it depending on the size of collection is good example of Stategy design pattern.

My design patterns table

If you haven't subsribed yet, you can subsribe below: