I need to know if the following lines of code represent a valid implementation of Strategy Pattern in Java 8:
public interface AIStrategy {
void play();
}
public class AIPlayer {
private AIStrategy aiStrategy;
public AIPlayer() {
}
public AIPlayer(AIStrategy aiStrategy) {
this.aiStrategy = aiStrategy;
}
public void changeStrategy(AIStrategy aiStrategy) {
this.aiStrategy = aiStrategy;
}
public void fight() {
aiStrategy.play();
}
}
public class App {
public static void main(String[] args) {
AIPlayer aiPlayer = new AIPlayer();
String difficulty;
Scanner sc = new Scanner(System.in);
difficulty = sc.next();
switch (difficulty) {
case "chill":
aiPlayer = new AIPlayer(()
-> System.out.println("The opponent is playing a chill strategy..."));
aiPlayer.fight();
break;
case "calculated":
aiPlayer = new AIPlayer(()
-> System.out.println("The opponent is playing a calculated strategy..."));
aiPlayer.fight();
break;
}
System.out.println("Is the opponent hurt?");
boolean isHurted = sc.nextBoolean();
if (isHurted) {
aiPlayer.changeStrategy(()
-> System.out.println("The opponent changed his strategy. Now he is having a holy rage!!"));
aiPlayer.fight();
}
}
}