Week Exercise 11

Reducing Coupling with Interfaces

Coupling occurs when the code in two classes or methods is so intertwined that changing one requires a change in the other. The code shown below, which can be found the GitHub repository for this assignment, is an example of coupling. If another movement mode like crawling is added, that would require a change to the CharacterDriver and Character classes.

Often we can limit coupling by isolating code changes to one method or class such as the driver. One way to do this is with interfaces.

public class Character {
    private String moveMode;
    public Character(String moveMode) {
        this.moveMode = moveMode;
    }
    public String getMoveMode() {
        return moveMode;
    }
    public void setMoveMode(String moveMode) {
        this.moveMode = moveMode;
    }
    public void move(){
        switch (moveMode){
            case "walk" -> System.out.println("Walking casually.");
            case "sneak" -> System.out.println("Sneaking stealthily.");
            case "run" -> System.out.println("Running hurriedly.");
        }
    }
}

public class CharacterDriver {
    static void main() {
        Character character = new Character("walk");
        Scanner scan = new Scanner(System.in);
        boolean quit = false;
        do{
            System.out.println("Current movement mode: "+character.getMoveMode());
            character.move();
            System.out.println("What would you like to do?");
            System.out.println("""
                    1. Change to walk.
                    2. Change to sneak.
                    3. Change to run.
                    Q. Quit
                    """);
            String input = scan.next().toLowerCase();
            switch (input) {
                case "1" -> character.setMoveMode("walk");
                case "2" -> character.setMoveMode("sneak");
                case "3" -> character.setMoveMode("run");
                case "q" -> {
                    System.out.println("Quitting");
                    quit = true;
                }
                default -> System.out.println("Invalid input.");
            }
        } while (!quit);
    }
}

Interfaces

Create the classes and interfaces shown in the following UML and refactor Character and CharacterDriver to use the interfaces. The move() and getMoveMode() of the implementing classes should replicate the output of the move() and getMoveMode() methods of the original Character class such that the output of your program, shown below, does not change.

In the README, answer the following question. Given the refactoring and incorporation of the interfaces, what changes would be needed if you wanted to add another movement mode such as crawling?

Exercise11UML.png

Current movement mode: walk
Walking casually.
What would you like to do?
1. Change to walk.
2. Change to sneak.
3. Change to run.
Q. Quit

2
Current movement mode: sneak
Sneaking stealthily.
What would you like to do?
1. Change to walk.
2. Change to sneak.
3. Change to run.
Q. Quit

3
Current movement mode: run
Running hurriedly.
What would you like to do?
1. Change to walk.
2. Change to sneak.
3. Change to run.
Q. Quit

1
Current movement mode: walk
Walking casually.
What would you like to do?
1. Change to walk.
2. Change to sneak.
3. Change to run.
Q. Quit

q
Quitting

Submission Instructions:

When you are done refactoring the code and have answered the question about adding the crawl movememnt in the README, add "DONE" to the README and commit and push.