For the strategy pattern, why do we need a context class to call the appropriate algorithm? Why can't we just do Strategy s = new OperationAdd()
? This way, we still use polymorphism.
Design – Why do we need a context class in strategy pattern
designdesign-patternsobject-oriented-designstrategy
Best Answer
In some languages, notably C# and Java, you can strategize, not the sorting algorithm, but the way the objects being sorted are compared for ordering purposes.
For example, in C#, you could say something like
and the MergeSort will treat the objects being sorted like imaginary numbers for ordering purposes.
Changing the sorting algorithm, however, requires knowledge of the entire collection, not just two of its members.