I am practicing the strategy pattern and trying to apply the code here. https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm
What I am confusing is I did not declare any static method in the Context
class as it showed that cannot do such the operation as I made a static reference to the non-static reference method.
I want to know why is this happening. Any help is highly appreciated.
Here is part of my interface and relevant code.
interface ArithmeticStratrgy
{
public int doOperation(int num1, int num2);
}
class AddOperateion implements ArithmeticStratrgy
{
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}
class Context
{
private ArithmeticStratrgy strategy;
public Context(ArithmeticStratrgy strategy) {
this.strategy = strategy;
}
public int executeArithmeticStrategy(int num1, int num2) {
// Cannot make a static reference to the non-static method
// doOperation(int, int) from the type ArithmeticStratrgy
return ArithmeticStratrgy.doOperation(num1, num2);
}
}