1
\$\begingroup\$

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();
        }
    }
}
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Yes, this is a good implementation of the strategy pattern. I don't know if the code you posted is just a simplification or not but I think that you have to prevent a NullPointerException when a client class doesn't specify the strategy to use. You can achieve this task in many ways, for example eliminating the empty constructor or inserting a default strategy in the empty constructor. Another elegant way would be making the strategy a parameter of the fight() method (obviously only if it used within the method):

public void fight(AIStrategy strategy) {
     strategy.player();
}

Making this way you will make the classes lighter, and it will be more difficult to get a NullPointerException.

public class App {

    public static void main(String[] args) {
        AIPlayer aiPlayer = new AIPlayer();
        AIStrategy stragey = () -> System.out.println("Default strategy");
        Scanner sc = new Scanner(System.in);
        String difficulty = sc.next(); 
        switch (difficulty) {
            case "chill":
                strategy = () -> System.out.println("The opponent is playing a chill strategy...");
                break;
            case "calculated":
                strategy = () -> System.out.println("The opponent is playing a calculated strategy...");
                break;
        }
        aiPlayer.fight(strategy);
        System.out.println("Is the opponent hurt?");
        boolean isHurted = sc.nextBoolean();
        if (isHurted) {
            strategy = () -> System.out.println("The opponent changed his strategy. Now he is having a holy rage!!"));
            aiPlayer.fight();
        }
        aiPlayer.fight(strategy);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.