/////////////////////////////////////////////////////////////////////// // Client.cpp - Strategy Example Code - Sorting // // Ver: 1.2 // // Language: Visual C++ 2010 // // Platform: Dell Dimension T7400, Win 7, Pro // // Author: Jim Fawcett, CSE776 - Design Patterns, Fall 2011 // // Source: Lei Zhang, lzhang17@syr.edu // // from internet - no longer have url // /////////////////////////////////////////////////////////////////////// /* Purpose: -------- Illustrates application of Strategy Pattern using different sorting strategies. Maintenance History: -------------------- ver 1.2 : 25 Oct 2011 - changed to random input and high resolution timer Ver 1.1 : 27 Jun 2010 - cosmetic modifications Ver 1.0 : 02 Jul 2007 - first release */ #include "Context.h" #include "HiResTimer.h" #include #include #include using namespace std; using namespace HRTimer; int main() { cout << "\n Pattern: Strategy Example Code" << "\n " << string(30, '=') << endl; // run for 20 and 20000 size_t count = 2000; HiResTimer timer; cout.setf(ios::floatfield, ios::fixed); cout << "\n Strategy initially configured as BubbleSort"<< "\n " << string(43, '-'); cout << "\n " << count << " random integers in [1," << count << "]" << endl; Context rec(new BubbleSort()); rec.recordGenerator(count); rec.display(); cout << endl; timer.Start(); rec.doSorting(); timer.Stop(); rec.display(); cout << " milliseconds for BubbleSort: " << static_cast(timer.ElapsedMicroseconds())/1000 << endl; cout << "\n Set strategy to InsertionSort"<< "\n " << string(29, '-'); cout << "\n " << count << " random integers in [1," << count << "]" << endl; rec.setSortS(new InsertionSort()); rec.recordGenerator(count); rec.display(); cout << endl; timer.Start(); rec.doSorting(); timer.Stop(); rec.display(); cout << " milliseconds for InsertionSort: " << static_cast(timer.ElapsedMicroseconds())/1000 << endl; cout << "\n Set strategy to QuickSort"<< "\n " << string(25, '-'); cout << "\n " << count << " random integers in [1," << count << "]" << endl; rec.setSortS(new QuickSort()); rec.recordGenerator(count); rec.display(); cout << endl; timer.Start(); rec.doSorting(); timer.Stop(); rec.display(); cout << " milliseconds for QuickSort: " << static_cast(timer.ElapsedMicroseconds())/1000 << endl; cout << "\n Set strategy to MergeSort" << "\n " << string(25, '-'); cout << "\n " << count << " random integers in [1," << count << "]" << endl; rec.setSortS(new MergeSort()); rec.recordGenerator(count); rec.display(); cout << endl; timer.Start(); rec.doSorting(); timer.Stop(); rec.display(); cout << " milliseconds for MergeSort: " << static_cast(timer.ElapsedMicroseconds())/1000 << "\n\n"; return 0; }