vendredi 19 novembre 2021

How to return out of command pattern in controller so that application stops running (Java)?

I am designing a controller using the command pattern. The controller has a while loop inside that is scanning for user input. If user input matches a specific String, then a command class is executed. Here is a code snippit:

  public Controller(Readable in, Appendable out) {
    this.out = out;
    this.scan = new Scanner(in);
    this.commandMap = this.generateCommands();
  }

public void go(Model m) {

      while (scan.hasNext()) {
        
        String input = scan.next();
        Command command = this.commandMap.get(input);
          command.do(m);
        }
}

I usually use return to stop the application. However, when I use return inside one of the Command classes, the application keeps running. I think it just goes back to this upper loop. By the way, all my commands are public void.

Is there a way to exit/close the application from within the command classes? Like a "super" return? Or do I need to no longer make them void and if/else the return in the controller.

Aucun commentaire:

Enregistrer un commentaire