# Java Notes

# Separating UI from Logic

One of the most important ideas in organizing programs is to separate the user interface (GUI, presentation, view and controller, ...) from the logic (model, engine, ...). There are two really good reasons for this.

1. The resulting program is conceptually simpler, and this means it is easier to work on, extend, refactor, understand, maintain, etc.
2. A different user interface or logic model can be added without affecting the other. For example, imagine that we've written a game, eg Connect4, and separated the graphical user interface from the underlying game logic. Why would we want to replace the GUI?
   - Maybe you want the program to run on a PDA or phone, which use a related, but different, Java graphical user interface, Java 2 Micro Edition. The new GUI can be written without concern for its affect on the logic model, which can very likely remain the same.
   - Perhaps you want to set up a Connect4 server so that the game could be played over the web, but use only one master copy of the logic on your server. There will be no GUI in this case; a server program like Tomcat or PHP will call on the (unchanged) logic module and build the appropriate web page to send back to the user.
   - A good reason to replace the logic is when you've written a new version that plays better or more efficiently.
   - It's hard to see why you might want to replace the GUI with a text mode interface for a game, but you might want to do this if you want a program to run as a Unix batch program.