6

I can not understand why to use Context module(which we will see in the following codes) in strategy design pattern, what its function? Let's see one part of the strategy design pattern.

public interface Strategy {
    public int doOperation(int num1, int num2);
}

public class OperationAdd implements Strategy {
    @Override 
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }
}

public class OperationSubstract implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 - num2;
    }
}

public class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public int executeStrategy(int num1, int num2) {
        return strategy.doOperation(num1, num2);
    }
}

From the codes above, we may call different algorithms by this way:

Context context = new Context(new OperationAdd());
context.executeStrategy(10,5);

which I can not understand completely, why could not call the child class directly but to use the Context layer. In my opinion, simply like this:

Strategy addStrategy = new OperationAdd();
addStrategy.doOperation(10, 5);
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Ivan
  • 661
  • 1
  • 8
  • 15
  • I still not understand it better. I think 'new Context(new OperationAdd()).executeStrategy(10,5)' and 'new OperationAdd().doOperation(10, 5)' make no difference. – Ivan Jan 13 '16 at 11:28

4 Answers4

4

Even though I did not like the example, I will explain the advantages of having Context.

Here Context has been used to wrap all strategies. If you properly use it, it can act as a mini Facade or Factory.

If I have to implement Context, I will create all Strategies once during construction and implement a Factory_Method to return which Strategy to be used based on Input parameter. Even we can avoid if else condition with this approach.

Once I get the strategy, I will simply call executeStrategy which hides internals of my strategy.

In absence of Context, the caller has to fetch concrete strategy before executing strategy. If you are calling Add or Subtract strategy 100 times in your application, you have to expose particular concrete strategy.

If I have Context, It will take care of providing right strategy by hiding my strategies and it can switch the strategies at runtime.

On a different note : Strategy_Pattern from Wikipedia provides good example. This example is may not be related to the question but it will provide good insight to understand effectiveness of Strategy pattern (especially the section : Strategy and open/closed principle)

For better understanding of Strategy pattern, have a look at this post:

Real World Example of the Strategy Pattern

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • ravindra explained it better,this is how object creation should be handled by hiding its implementation and passing the parameter for the object you want to create. – Manish Singh Jan 13 '16 at 04:43
2

It's just demonstrating that you can change the functionality of a class by using composition and the strategy pattern. Not the best example.

Context will return 5 or 15 given the same inputs and executing the same method, without changing the code within the context object. You just have to specify a strategy to use.

Slihp
  • 763
  • 5
  • 12
0

Context decides at runtime which strategy has to be executed. If you create a concrete strategy instance yourself and invoke it then it becomes a compile time thing and not exactly what the strategy pattern aims for.

Also, Strategy Pattern's Context has sort of become secondary in the post dependency-injection era.

Before dependency injection came into picture, a class was needed at runtime which would do an if-else based on client needs and select the appropriate concrete strategy implementation. This class was called as Context.

When dependency injection came into picture then scenario specific concrete strategy instances could be inserted at runtime. So, the container doing this dependency injection started playing the role of the Context.

Dhruv Rai Puri
  • 1,335
  • 11
  • 20
0

The purpose of using any design pattern is maximum cohesion and loose coupling. In the Strategy design pattern, the client interacts with the context and set the algorithm name or strategy name. So It maintains loose coupling between client and implementation.

So we can any number of implementation for an interface and client don't need to be aware of this because the client just provides algorithm and get access to required implementation methods. So to provide loose coupling and maintaining high abstraction, it's best to use context so that implementation can be decided at runtime using runtime polymorphism

user15003460
  • 91
  • 1
  • 6