Here, nock yourself out. Same idea, but with sorting algorithms.
Here are the analogies (comparisons):
- Class Sorter: an abstract vehicle. So you can also define medical and transport vehicles instead of only fight-vehicles.
- Class BubbleSorter (derrived): Cheap attack vehicle.
- Class CombSorter (derrived). Expenise but fast attack vehicle.
- Class SortHandle: All kinds of Strategies.
- Class ArraySortHandle (derrived) : Various Attack Strategies.
- Class DescendSortHandle(derrived from derrived): Our first attack strategy-> "Aim low when shooting".
- Class AscendSortHandle(derrived from derrived): Our second attack strategy-> "Aim high when shooting".
Analyze well and do the tasks with the previous codes I gave to you. Don't touch this one. Just run it and observe.
Code:
//**** StrategyPatternNeatSample.cpp : Entry point for this app.
#include"Sorter.h"
#include "BubbleSorter.h"
#include "CombSorter.h"
#include "AscendSortHandle.h"
#include "DescendSortHandle.h"
#include <iostream>
#include <string>
using namespace std;
string sLeaderTypes[7]={ "President", "Queen", "Warlord", "Ceasar",
"Minister", "Emperor", "Archduke"};
int elements = sizeof sLeaderTypes / sizeof *sLeaderTypes;
void Print(string* array, int size)
{
for(int i=0 ; i< size ; ++i)
cout<<array[i]<<endl;
}
void SortAndPrint(Sorter* sorter, SortHandle* handle)
{
sorter->Sort(handle);
Print(sLeaderTypes, elements);
cout << endl;
}
int main()
{
AscendSortHandle<string> ascendHandle(sLeaderTypes, elements);
DescendSortHandle<string> descendHandle(sLeaderTypes, elements);
BubbleSorter bSort;
CombSorter cmbSort;
cout << "*** Ascending Bubble Sort ***\n\n";
SortAndPrint(&bSort, &ascendHandle);
cout << "\n\n*** Descending Comb Sort ***\n" << endl;
SortAndPrint(&cmbSort, &descendHandle);
return EXIT_SUCCESS;
}
//**** Sorter.h ****
#include "SortHandle.h"
class Sorter
{
public:
virtual void Sort(SortHandle*) = 0;
};
//**** BubbleSorter.h ****
class BubbleSorter : public Sorter
{
public:
BubbleSorter() {}
virtual ~BubbleSorter() {}
public:
void Sort(SortHandle* sortHandle)
{
int size = sortHandle->GetSize();
for(int i = size; i>=0; --i)
{
for(int j= 0 ; j<size-1 ; j++)
{
if( sortHandle->OutOfOrder(j, j+1) )
sortHandle->Swap(j,j+1);
}
}
}
};
//**** CombSorter.h ****
class CombSorter : public Sorter
{
public:
CombSorter() {}
virtual ~CombSorter() {}
void Sort(SortHandle* sortHandle)
{
int size = sortHandle->GetSize();
int gap = size;
for (;;)
{
gap = NewGap(gap);
bool swapped = false;
for(int j= 0 ; j<size-gap ; j++)
if( sortHandle->OutOfOrder(j, j+gap))
{
sortHandle->Swap(j,j+gap);
swapped = true;
}
if(gap == 1 && !swapped)
break;
}
}
static int NewGap(int gap)
{
gap = (gap * 10) / 13;
if (gap == 9 || gap == 10)
gap = 11;
if (gap < 1)
gap = 1;
return gap;
}
};
// **** SortHandle.h ****
#ifndef SORTHANDLE_H
#define SORTHANDLE_H
class SortHandle
{
public:
virtual void Swap(int i, int j) = 0;
virtual bool OutOfOrder(int i, int j) = 0;
virtual int GetSize() = 0;
};
#endif // SORTHANDLE_H
//**** ArraySortHandle.h ****
#ifndef ARRAYSORTHANDLE_H
#define ARRAYSORTHANDLE_H
#include "SortHandle.h"
template<class T> class ArraySortHandle : public SortHandle
{
protected:
ArraySortHandle(T* array, int elements)
: itsArray(array), itsElements(elements) {}
virtual int GetSize(){ return itsElements; }
virtual void Swap(int i, int j)
{
T tmp = itsArray[i];
itsArray[i]=itsArray[j];
itsArray[j]=tmp;
}
T* itsArray;
int itsElements;
};
#endif //ARRAYSORTHANDLE_H
//**** DescendSortHandle.h ****
#include "ArraySortHandle.h"
template<class T> class DescendSortHandle : public ArraySortHandle<T>
{
public:
DescendSortHandle(T* array, int elements)
: ArraySortHandle<T>(array, elements) {}
public:
virtual bool OutOfOrder(int i, int j) { return itsArray[i]<itsArray[j]; }
};
//**** AscendSortHandle.h ****
#include "ArraySortHandle.h"
template<class T> class AscendSortHandle : public ArraySortHandle<T>
{
public:
AscendSortHandle(T* array, int elements)
: ArraySortHandle<T>(array, elements) {}
virtual bool OutOfOrder(int i, int j){ return itsArray[i]>itsArray[j]; }
}; Remember, you don't need templates. I used templates so my code can sort any sortable type.
You only need to let your vehicles print out that they are attacking. Nothing more.
Now I am really tired and its time I go (party).
Good luck!
P.S.
This type of OO implementation is called the
Strategy Pattern.
What a coincidence huh?
