We can represent sorting algorithms as interchangeable objects (interchangeable because they all implement the same interface.)
Instead of hiding the algorithm used to sort lists, lists could allow clients to inspect and modify the algorithm by providing getter and setter methods for the sorting strategy.
There are two types of strategies: dedicated and non-dedicated. In this diagram we are showing the non-dedicated style in which the list to be sorted passes itself as a parameter to the strategy. This means that one strategy can be shared by multiple lists.
A dedicated strategy maintains a field containing a pointer to the list to be sorted. In this case the sort method doesn't need a parameter.
#define cap 100
class List
{
private:
Comparable* members[cap];
private int size;
SortingStrategy* strategy;
friend class SortingStrategy;
public:
void setStrategy(SortingStrategy* s)
{
strategy = s;
}
SortingStrategy* getStrategy()
{
return strategy;
}
void sort()
{
strategy->sort(this);
}
};