Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In principle, the GUI interface to the laundry simulation (implemented by SimLaundryApplication and affiliated classes) could have been written as an alternate implementation of the IOProcess interface. But in Java GUIs (and GUIs in other languages) essentially all code that accesses and updates GUI components must run in the event-handling thread of the Java Virtual Machine. When the program runs in TerminalIO} mode, the {{Main class creates a student and runs the simulation (using the simluate method in Student) in the main thread. A GUI implementation of IOProcess would have to install closures (called listeners or callbacks) in the Student class that would run whenever the state of Acker's world changed (as displayed in the GUI window). The Student class would have to be extended to include methods that accept such listeners and code to execute the appropriate listeners whenever the state of some object in Acker's world changed. But note that these listeners will run in the main thread unless the code in the listeners specifically addresses this issue. The listener code can send closures (technically anonymous classes implementing the Runnable interface) to the event-handling thread using the methods invokeLater and invokeAndWait.

But for simple GUIs applications like this laundry simulation, it is often simpler to write the GUI as an application that is driven by GUI events. The initiatlization initialization of the GUI creates an Acker Student object and associated DoCommandVisitor. Each GUI event triggers the execution of DoCommandVisitor; in some cases, such as reading input from a file, it triggers the execution of DoCommandVisitor on a stream of Commands. In essence, the event-handling loop built-in to the Java Swing framework is used to drive the computation rather than a separate loop in the main thread such as the one in the simulate method in Student. writing the GUI as an implementation of IOProcess means the program has two loops in separate threads--a main program loop in the simulate method of Student and the event-handling thread supporting the processing of GUI inputs. The SimLaundryApplication dispenses with the main program loop by absorbing the application (often called the model) into the GUI (often called the view),

...