对战略设计模式感到困惑 [英] Confused about strategy design pattern
问题描述
我不明白为什么要在策略设计模式中使用Context模块(我们将在下面的代码中看到什么),它的功能是什么?我们来看看战略设计模式的一部分。
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上下文(策略策略){
this.strategy = strategy;
}
public int executeStrategy(int num1,int num2){
return strategy.doOperation(num1,num2);
}
}
从上面的代码,我们可能会调用不同的算法这样:
Context context = new Context(new OperationAdd());
context.executeStrategy(10,5);
我完全不能理解,为什么不能直接调用子类,而是使用上下文层。在我看来,只是这样:
策略addStrategy = new OperationAdd();
addStrategy.doOperation(10,5);
即使我不喜欢这个例子,解释具有上下文的优点。
这里上下文
已被用于包装所有策略。如果您正确使用它,它可以作为一个迷你立面或工厂。
如果我必须实现上下文
,我将在构建期间创建所有策略一次,并实现 Factory_Method 根据输入参数返回要使用的策略
。即使我们可以避免 if else
条件与这个方法。
一旦得到策略,我将简单地调用 executeStrategy
隐藏我的战略的内部。
在没有 如果我有 另一个说明: Strategy_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. From the codes above, we may call different algorithms by this way: 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:
Even though I did not like the example, I will explain the advantages of having Context. Here If I have to implement Once I get the strategy, I will simply call In absence of If I have 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 : For better understanding of Real World Example of the Strategy Pattern 这篇关于对战略设计模式感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!上下文
的情况下,调用者必须在执行策略之前获取具体的策略。如果您在应用程序中调用添加
或减除策略100次,则必须公开具体的具体策略。 p>
上下文
,它将通过隐藏我的策略来处理提供正确的策略,它可以将策略切换到
战略和开放/封闭原则
)
策略
模式,看看这篇文章:
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);
}
}
Context context = new Context(new OperationAdd());
context.executeStrategy(10,5);
Strategy addStrategy = new OperationAdd();
addStrategy.doOperation(10, 5);
Context
has been used to wrap all strategies. If you properly use it, it can act as a mini Facade or Factory. 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.executeStrategy
which hides internals of my strategy. 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.Context
, It will take care of providing right strategy by hiding my strategies and it can switch the strategies at runtime.Strategy and open/closed principle
)Strategy
pattern, have a look at this post: