 // Purpose.  Strategy (template         #include <iostream>
 //                     approach)        #include <cstdlib>
 // A template can be used to configure  #include <ctime>
 // a client with a Strategy.  This      using namespace std;
 // technique is appropriate if: 1) the
 // Strategy can be selected at com-     template<class STRATEGY>
 // pile-time, and 2) it does not have   class Stat {
 // to be changed at run-time.  With a   public:
 // template, there is no need to spe-      void readVector( int v[], int n ) {
 // cify the interface in a SortImp            imp_.sort( v, n );
 // base class.  The Stat class now has        min_ = v[0];   max_ = v[n-1];
 // an instance of the sort object, in-        med_ = v[n/2]; }
 // stead of a ptr to the base class.       int getMin() { return min_; }
 // The inheritance approach offers         int getMax() { return max_; }
 // more options and expressiveness.        int getMed() { return med_; }
 // The template approach offers mildly  private:
 // better efficiency.                      int       min_, max_, med_;
                                            STRATEGY  imp_;
 class Stat {     /* Bubble sort */      };
 public:
    void readVector( int v[], int n ) {  class SortBubble {
       sort_( v, n );                    public:
       min_ = v[0];   max_ = v[n-1];        void sort( int v[], int n );
       med_ = v[n/2]; }                  };
    int getMin() { return min_; }
    int getMax() { return max_; }        class SortShell {
    int getMed() { return med_; }        public:
 private:                                   void sort( int v[], int n );
    int min_, max_, med_;                };
    void sort_( int v[], int n ) {
       for (int i=n-1; i > 0; i--)       #include "strategy2.inc"
          for (int j=0; j < i; j++)
             if (v[j] > v[j+1]) {        void main( void ) {
                int t = v[j];               const int NUM = 9;
                v[j] = v[j+1];              int       array[NUM];
                v[j+1] = t; }               time_t    t;
       cout << "Bubble: ";                  srand((unsigned) time(&t));
       for (int k=0; k < n; k++)            cout << "Vector: ";
          cout << v[k] << ' ';              for (int i=0; i < NUM; i++) {
       cout << endl;                           array[i] = rand() % 9 + 1;
    }                                          cout << array[i] << ' '; }
 };                                         cout << endl;

 void main( void ) {                        Stat<SortBubble>  obj;
    const int NUM = 9;                      obj.readVector( array, NUM );
    int       array[NUM];                   cout << "min is " << obj.getMin()
    time_t    t;                               << ", max is " << obj.getMax()
    srand((unsigned) time(&t));                << ", median is " << obj.getMed()
    cout << "Vector: ";                        << endl;
    for (int i=0; i < NUM; i++) {
       array[i] = rand() % 9 + 1;           Stat<SortShell>  two;
       cout << array[i] << ' '; }           two.readVector( array, NUM );
    cout << endl;                           cout << "min is " << two.getMin()
                                               << ", max is " << two.getMax()
    Stat  obj;                                 << ", median is " << two.getMed()
    obj.readVector( array, NUM );              << endl;
    cout << "min is " << obj.getMin()    }
       << ", max is " << obj.getMax()
       << ", median is " << obj.getMed() // Vector: 3 5 4 9 7 1 4 9 2
       << endl;                          // Bubble: 1 2 3 4 4 5 7 9 9
 }                                       // min is 1, max is 9, median is 4
                                         // Shell:  1 2 3 4 4 5 7 9 9
 // Vector: 6 9 9 8 6 5 7 9 2            // min is 1, max is 9, median is 4
 // Bubble: 2 5 6 6 7 8 9 9 9
 // min is 2, max is 9, median is 7

