The following code illustrate strategy pattern using a real world example of encapsulates sorting algorithms in the form of sorting objects. It looks 90% the same as the Duck class example from my last few posts, except two differences:
- the duck class has duck subclasses as well such as MallardDuck, whereas in this example, the SortedList class doesn't have subclasses defined.
- in this example there is only one Strategy - SortStrategy, whereas in the duck example, there are more than one behavior such as IFlyBehavior/IQuackBehavior
But in essence, they are the same. Both are good examples of illustrating strategy pattern, they can be extended easily because they inherit this flexible structure.
// "test"
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Strategy.RealWorld
{
class MainApp
{
static void Main()
{
SortedList studentRecords = new SortedList();
studentRecords.Add("Samual");
studentRecords.Add("Jimmy");
studentRecords.Add("Sandra");
studentRecords.Add("Vivek");
studentRecords.Add("Anna");
studentRecords.SetSortStrategy(new QuickSort()); // similar to setFlyBehavior(new FlyNoWay());
studentRecords.Sort();
studentRecords.SetSortStrategy(new ShellSort());
studentRecords.Sort();
studentRecords.SetSortStrategy(new MergeSort());
studentRecords.Sort();
// Wait for user Console.Read();
}
}
// "Strategy" Here it uses an abstract class rather than IFlyBehavior, it achieves the same effect. Note: defining an abstract class with all the abstract members is similar to defining an interface.
abstract class SortStrategy
{
public abstract void Sort(ArrayList list);
}
// "ConcreteStrategy" Similar to FlyNoWay/FlyWithWings
class QuickSort : SortStrategy
{
public override void Sort(ArrayList list)
{
list.Sort(); // Default is Quicksort
Console.WriteLine("QuickSorted list ");
}
}
// "ConcreteStrategy" Similar to FlyNoWay/FlyWithWings
class ShellSort : SortStrategy
{
public override void Sort(ArrayList list)
{
//list.ShellSort(); not-implemented
Console.WriteLine("ShellSorted list ");
}
}
// "ConcreteStrategy" Similar to FlyNoWay/FlyWithWings
class MergeSort : SortStrategy
{
public override void Sort(ArrayList list)
{
//list.MergeSort(); not-implemented
Console.WriteLine("MergeSorted list ");
}
}
// "Context" Similar to Duck. The duck example is a bit complex than this since it has duck subclasses as well such as MallardDuck.
class SortedList
{
private ArrayList list = new ArrayList();
private SortStrategy sortstrategy; //Similar to "IFlyBehavior flyBehavior" defined in Duck class, the duck example is a bit more complex since it has more than one behavior defined here.
public void SetSortStrategy(SortStrategy sortstrategy)
{ this.sortstrategy = sortstrategy; }
public void Add(string name)
{ list.Add(name); }
public void Sort()
{
sortstrategy.Sort(list); // Display results
foreach (string name in list)
{ Console.WriteLine(" " + name); }
Console.WriteLine();
}
}
}
Output:
QuickSorted list
Anna
Jimmy
Samual
Sandra
Vivek
ShellSorted list
Anna
Jimmy
Samual
Sandra
Vivek
MergeSorted list
Anna
Jimmy
Samual
Sandra
Vivek