对战略设计模式感到困惑 [英] Confused about strategy design pattern

查看:104
本文介绍了对战略设计模式感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么要在策略设计模式中使用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 隐藏我的战略的内部。



在没有上下文的情况下,调用者必须在执行策略之前获取具体的策略。如果您在应用程序中调用添加减除策略100次,则必须公开具体的具体策略。 p>

如果我有上下文,它将通过隐藏我的策略来处理提供正确的策略,它可以将策略切换到



另一个说明 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.

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

解决方案

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

这篇关于对战略设计模式感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆