Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about strategy design pattern

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);
like image 484
Ivan Avatar asked Sep 26 '22 16:09

Ivan


2 Answers

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

like image 67
Ravindra babu Avatar answered Oct 05 '22 23:10

Ravindra babu


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.

like image 29
Slihp Avatar answered Oct 05 '22 22:10

Slihp