Strategy Pattern Code Review

Use the Strategy Pattern

Code Review

Code Walkthrough

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(); } }