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.

Strategy pattern design

  • 3
    I actually had to read the Wikipedia article to see what you are talking about. They show it in the class diagram but none of the code examples use it, unless they mean the code that calls the strategy. I am a bit unclear as to what role it plays given that I have never used a "context" for a strategy (which is one of the really useful design patterns). – user22815 Nov 18 '15 at 5:12

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

var sort = new MergeSort(new ImaginaryNumberComparator());

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.


UML class diagram at wikipedia article shows Context to include strategy by composition. So, the class, you use Strategy inside, IS a context.

class MyClass() {
  public function sort() {
    SortStrategy s=new MergeSort();

Here MyClass is a context for the strategy.

  • I think context object is a thing of its own, the shell where strategy is used is not really the context. This is what adds to the confusion. – zadane Oct 17 '18 at 21:19

Within the Strategy patterns, the Context class is the class that owns the (concrete) Strategy object.

The reason for calling that class Context rather than Client (or something similar) is because the Strategy Pattern is formulated in such a way that this class provides the contextual information that the various Strategy implementations need.
If you have a sorting strategy , then the Strategy Pattern states that the class that holds the actual sorting algorithm to use will also pass the collection that needs to be sorted to the sorting strategy and any comparison object/function that might be needed.

  • In your last paragraph, why does the strategy pattern assume that? – Armon Safai Nov 18 '15 at 23:02
  • @ArmonSafai: I have updated my answer. It wasn't really that the patterns assumes it, but rather that the pattern describes the interaction between the classes that way. – Bart van Ingen Schenau Nov 19 '15 at 7:46
  • yea but why? why do we call context class, Context c1=new Context(new BubbleSort()) rather than Sort s1=new BubbleSort() in main? What is the benefit of using the Context class? – Armon Safai Nov 19 '15 at 8:16
  • 1
    Another example might help? Assume you program a game, where you have an AI (Context) whose strength should vary. Then you'll have an AttackStrategy which is implemented by (e.g.) RandomStrategy and MinimaxStrategy. 'AI ai = new AI(new MinimaxStratey())' ... I think this would make less sense without the Context. – Beko Nov 23 '15 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.