https://www.javatpoint.com/strategy-pattern
I have heard that we can't initiate (make objects) of interfaces, but in Strategy Pattern, we are creating an object of Strategy interface.
Code
public interface Strategy {
public float calculation(float a, float b);
}
public class Context {
**private Strategy strategy;**
public Context(Strategy strategy){
this.strategy = strategy;
}
public float executeStrategy(float num1, float num2){
return strategy.calculation(num1, num2);
}
}
In context class, we have created an object of Strategy interface. How is this possible as interfaces can't be instantiated.
Aucun commentaire:
Enregistrer un commentaire