Unable to understand strategy template

I follow the example of a strategy template from here

Everything in the textbook 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); } } 

Thus, the Context class expects a strategy argument in its constructor.

Strategy Definition:

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

The above interface, the Context class expects an object of type Strategy. In the StrategyPatternDemo class, we do:

 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 completely confused as we cannot initialize the interface as defined:

An interface differs from a class in several ways, including:

You cannot create an interface.

How exactly does this Context context = new Context(new OperationAdd()); sent as argument public Context(Strategy strategy){ this.strategy = strategy; } public Context(Strategy strategy){ this.strategy = strategy; }

+5
source share
3 answers

The OperationAdd , OperationSubstract and OperationMultiply classes implement the Strategy interface. Therefore, instances of these classes can be passed to the Context constructor, which expects an object of type Strategy .

As OperationAdd , OperationSubstract and OperationMultiply implements the Strategy interface, they all belong to this type.

+4
source

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 “operational” classes that implement the Strategy interface. A class that implements an interface is basically an “actual instance” of that interface.

You might think so if it is clearer:

  • An interface is a representation of "behavior";
  • a class that implements this interface is one that can "behave like an interface state";
  • A method that takes an “interface” as an argument is a method that simply wants an object that “can behave” as an interface state and does not have an object class that is passed as an argument against the real class.
+2
source

This applies to interfaces and classes. List is an interface, ArrayList and LinkedList are classe implementing this interface.

It makes sense to have:

 List<String> books = new ArrayList<>(); void printList(List<String> list) { ... } printList(books); 

This allows you to be universal, being able to change the implementation.

+2
source

Source: https://habr.com/ru/post/1233023/


All Articles