简体   繁体   English

无法理解战略模式

[英]Unable to understand Strategy pattern

I am following an example of strategy pattern from here 我从这里开始关注战略模式的一个例子

Everything in the tutorial is clear but this: 教程中的所有内容都很清楚但是:

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

So the Context class expects a Strategy argument in its constructor. 因此Context类在其构造函数中需要一个Strategy参数。

The definition of Strategy is: 战略的定义是:

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

The above being an interface, the Context Class expects an object of type Strategy. 上面是一个接口,Context Class需要一个Strategy类型的对象。 In the StrategyPatternDemo class we do: 在StrategyPatternDemo类中,我们执行以下操作:

public class StrategyPatternDemo {
   public static void main(String[] args) {
      Context context = new Context(new OperationAdd());        
      System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationSubstract());      
      System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationMultiply());       
      System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
   }
}

I am utterly confused as we cant init an interface according to the definition: 我完全感到困惑,因为我们无法根据定义初始化接口:

An interface is different from a class in several ways, including: 接口在几个方面与类不同,包括:

You cannot instantiate an interface. 您无法实例化接口。

How exactly is this Context context = new Context(new OperationAdd()); 这个Context context = new Context(new OperationAdd());究竟是多么Context context = new Context(new OperationAdd()); sent as an argument to public Context(Strategy strategy){ this.strategy = strategy; } 作为public Context(Strategy strategy){ this.strategy = strategy; }的论据发送public Context(Strategy strategy){ this.strategy = strategy; } public Context(Strategy strategy){ this.strategy = strategy; }

The classes OperationAdd , OperationSubstract and OperationMultiply all implement the interface Strategy . OperationAddOperationSubstractOperationMultiply都实现了接口Strategy Therefore instances of those classes can be passed into the constructor of Context which expects an object of type Strategy . 因此,可以将这些类的实例传递给Context的构造函数,该构造函数需要Strategy类型的对象。

As OperationAdd , OperationSubstract and OperationMultiply implement the interface Strategy , they are all of that type. OperationAddOperationSubstractOperationMultiply实现接口Strategy ,它们都是那种类型。

You are probably missing these lines, at the beginning of the example: 您可能在示例的开头缺少这些行:

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

Here you can see that there are some "operation" classes that implement the Strategy interface. 在这里你可以看到有一些实现Strategy接口的“操作”类。 A class that implements an interface is basically an "actual instance" of that interface. 实现接口的类基本上是该接口的“实际实例”。

You may think this way, if it's clearer to you: 你可以这样想,如果你更清楚的话:

  • an Interface is a representation of a "behaviour"; 界面是“行为”的表示;
  • a class that implements that interface is something that can "behave like the interface states"; 实现该接口的类可以“表现得像接口状态”;
  • a method that accept an "interface" as an argument is a method that simply wants an object that "can behave" like the interface states, and it doesn't mind of the actual class of the object that is passed as an argument. 接受“接口”作为参数的方法是一种简单地想要一个“可以像行为”一样的对象的方法,并且它不介意作为参数传递的对象的实际类。

This is about interfaces and classes. 这是关于接口和类。 List is an interface, ArrayList and LinkedList are classe implementing that interface. List是一个接口, ArrayListLinkedList是实现该接口的classe。

It makes sense to have: 有:有意义:

List<String> books = new ArrayList<>();

void printList(List<String> list) { ... }

printList(books);

This allows being generic, being able to change the implementation. 这允许通用,能够改变实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM