Printed from www.rmfusion.com A Developer website designed for Developers

Strategy Pattern Code Review

Code Download

Define the Pattern Interface and Handler Objects

Define the IStrategy interface to be used as the "blueprint" for the main application classes.

interface IStrategy { void Sort(List<String> list); }

This interface defines the Sort() method. The Sort() method represents the different sort functions that can be applied.

The QuickSortStrategy class (object) represents a sort pattern that implements a quick sort algorithm.

class QuickSortStrategy : IStrategy { public void Sort(List<string> list) { list.Sort(); } }

The MergeSortStrategy class (object) represents a sort pattern that implements a merge sort algorithm.

class MergeSortStrategy : IStrategy { public void Sort(List<String> list) { String[] dataArray = this.MergeSort(list.ToArray()); list.Clear(); list.AddRange(dataArray); } private String[] SplitArray(String[] data, Int32 lower, Int32 upper) { String[] sub = new String[upper - lower + 1]; for (Int32 i = lower; i <= upper && i < data.Length; i++) { sub[i - lower] = data[i]; } return sub; } private String[] MergeSort(String[] data) { if (data.Length == 1) return(data);
Int32 middle = data.Length / 2;
String[] left = this.MergeSort(this.SplitArray(data, 0, middle - 1)); String[] right = this.MergeSort(this.SplitArray(data, middle, data.Length - 1)); String[] result = new String[data.Length];
Int32 dPtr = 0; Int32 lPtr = 0; Int32 rPtr = 0;
while (dPtr < data.Length) { if (lPtr == left.Length) { result[dPtr] = right[rPtr]; rPtr++; } else if (rPtr == right.Length) { result[dPtr] = left[lPtr]; lPtr++; } else if(String.Compare(left[lPtr],right[rPtr]) < 0) { result[dPtr] = left[lPtr]; lPtr++; } else { result[dPtr] = right[rPtr]; rPtr++; } dPtr++; } return(result); } }

The ShellSortStrategy class (object) represents a sort pattern that implements a shell sort algorithm.

class ShellSortStrategy : IStrategy { public void Sort(List<string> list) { String[] dataArray = this.ShellSort(list.ToArray()); list.Clear(); list.AddRange(dataArray); } private String[] ShellSort(String[] data) { Int32 size = data.Length; Int32 increment = size;
while (increment >= 1) { increment /= 3; if(increment < 1) increment = 1;
for(Int32 i = 0; i < increment; i++) { Int32 j = i + increment;
while(j <= (size - 1)) { Int32 k = j;
while(String.Compare(data[k - increment], data[k]) > 0) { String temp = data[k - increment]; data[k - increment] = data[k]; data[k] = temp; k = k - increment; if ((k-increment) < 0) break; } j+=increment; } } if(increment == 1) break; } return (data); } }

Define the Context Object

The Context class (object) stores the application data and applies the sort strategy selected against the application data.

class Context { private List<String> list = new List<String>(); private IStrategy sortStrategy;
public Context(IStrategy sortStrategy) { this.sortStrategy = sortStrategy; }
public void AddItem(String name) { list.Add(name); } public void SetSortStrategy(IStrategy sortStrategy) { this.sortStrategy = sortStrategy; } public void Sort() { this.sortStrategy.Sort(list);
foreach (String name in list) { Console.WriteLine(name); } } }

Use the Strategy Pattern

The Program class creates an instance of the Context class with a default sort stategy of quick sort applied. The sort strategy is then changed to use the merge sort strategy and finally the shell sort strategy is applied.

class Program { static void Main(string[] args) { IStrategy quickSort = new QuickSortStrategy();
Context data = new Context(quickSort); data.AddItem("Samual"); data.AddItem("Jimmy"); data.AddItem("Sandra"); data.AddItem("Vivek"); data.AddItem("Anna"); data.AddItem("Jason");
Console.WriteLine(String.Format("*** Quick Sort applied {0}***", DateTime.Now.ToString("hh:mm:ss:ms:ns:ps"))); data.Sort(); Console.WriteLine(String.Format("*** Quick Sort applied {0}***", DateTime.Now.ToString("hh:mm:ss:ms:ns:ps"))); quickSort = null;
IStrategy mergeSort = new MergeSortStrategy(); data.SetSortStrategy(mergeSort); Console.WriteLine(String.Format("\n*** Merge Sort applied {0}***", DateTime.Now.ToString("hh:mm:ss:ms:ns:ps"))); data.Sort(); Console.WriteLine(String.Format("*** Merge Sort applied {0}***", DateTime.Now.ToString("hh:mm:ss:ms:ns:ps"))); mergeSort = null;
IStrategy shellSort = new ShellSortStrategy(); data.SetSortStrategy(shellSort); Console.WriteLine(String.Format("\n*** Shell Sort applied {0}***", DateTime.Now.ToString("hh:mm:ss:ms:ns:ps"))); data.Sort(); Console.WriteLine(String.Format("*** Shell Sort applied {0}***", DateTime.Now.ToString("hh:mm:ss:ms:ns:ps"))); shellSort = null;
Console.Read(); } }