Confused about strategy design pattern

牧云@^-^@ 提交于 2019-12-13 13:10:06

问题


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);

回答1:


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




回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/34757559/confused-about-strategy-design-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!