 |
Here is the source for the simple calculator shown at the left.
It's divided into three source files.
- Main (Calc.java) - A simple main program.
- User interface (CalcGUI.java) - This is
implemented as a subclass of JFrame. It both builds the interface
the user sees (the "view), and handles the events coming from the
buttons (the "controller").
- Model (CalcLogic.java) - This is where the actual calculations
take place. Altho this simple example doesn't show the full power
of separating the business logic (often called the "model") from
the user interface, there are many advantages in larger programs.
- It is simpler for the developer to work with.
- It can be used with many kinds of interfaces without changes. Eg,
a GUI interface, a command-line interface, or a web-based interface.
- The model can be changed (eg, to work with BigInteger) without
changing the user interface. Of course, some changes may require
interface changes, but the separation makes this easier.
|