Strategy Pattern

The strategy pattern is typically used when your programmer's algorithm should be interchangeable with different variations of the algorithm. For example, if you have code that sorts array, under certain circumstances, you might want to create QuickSort and under other circumstances, you might want to create Merge Sort.

The strategy pattern is usually implemented by declaring an abstract base class with an algorithm method, which is then implemented by inheriting concrete classes. At some point in the code, it is decided what concrete strategy is relevant; it would then be instantiated and used wherever relevant.

Following example shows how a ArrayList with diffrent sort algorithms.

using System;
using System.Collections;
using NUnit.Framework;

namespace DesignPatterns
{
    // Strategy class: SortStrategy
    // Define the general interface common to all supported algorithms
    public abstract class SortStrategy
    {
        public abstract void Sort(ArrayList list);
    }

    // Concrete class QuickSort
    public class QuickSort : SortStrategy
    {
        public override void Sort(ArrayList list)
        {
            // Sort the list using a quicksort algorithm
        }
    }

    // Concrete class MergeSort
    public class MergeSort : SortStrategy
    {
        public override void Sort(ArrayList list)
        {
            // Sort the list using a mergesort algorithm
        }
    }

    // Context class
    public class SortedList
    {
        private ArrayList list = new ArrayList();
        private SortStrategy sortstrategy;

 public SortedList(SortStrategy sortstrategy)
        {
            this.sortstrategy = sortstrategy;)
   
 }
       
        public void Add(string name)
        {
            list.Add(name);
        }

        public void Sort()
        {
            sortstrategy.Sort(list);
        }
    }

    [TestFixture]
    public class StrategyClassTestFixture
    {
        [Test]
        public void TestSortedList()
        {
            SortedList sortedList = new SortedList(new QuickSort());

            sortedList.Add("mitchel");
            sortedList.Add("gregg");
            sortedList.Add("steve");
            sortedList.Add("rob");

        }
    }
}

This article is part of the GWB Archives. Original Author: Prasanna Krishnan

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.