Comments About AddEmUp
Greetings All --
Here's an example of how one can design a project so that the "guts" can be used both in a command-line context as well as in a GUI context. The "trick" is to be certain that you isolate the mechanics of the operations to be performed in the "application" from the actual "driving" of the application. The phrase that describes this kind of design is MVC, which is short for "Model-View-Controller." Here's an abbreviated definition of MVC from WhatIs.com:
In object-oriented programming development,
model-view-controller (MVC) is the name of a methodology or design pattern for
successfully and efficiently relating the user interface to underlying data
models. The model-view-controller pattern proposes three main components:
· A Model, which represents the underlying, logical structure of data in a software application and the high-level class associated with it. This object model does not contain any information about the user interface.
· A View, which is a collection of classes representing the elements in the user interface (all of the things the user can see and respond to on the screen, such as buttons, display boxes, and so forth)
· A Controller, which represents the classes connecting the model and the view, and is used to communicate between classes in the model and view.
Let me illustrate this with a small example. Let's say that you need to build a tiny "database" that contains positive real numbers. We'll implement the database as an array plus a couple additional items, like the current number of values and the average of all the values. The database needs the ability to insert numbers into the array, to calculate the average, and to display its contents. We'll "encapsulate" all this in a class called Data. We'll need a small application that runs from the command-line that "drives" Data. We'll call it AddEmUp. It will contain the main method. We'll implement this much without worrying very much about MVC design; but, we will keep the database-related mechanisms separate from the driving mechanisms by keeping the two functions in separate classes in separate files.
Then we decide to build a GUI for this database. We'll encapsulate the GUI in a class called GUI. It will contain all the mechanisms necessary to drive Data, but using a graphical interface instead of a command-line interface. We'll need a small application that runs the GUI version. We'll call it AddEmUpGUI. It will contain the main method.
What's available now
Rather than showing you the completed implementation, we'll present the state of development after the following has been accomplished:
The Doit.bat "batch file" runs AddEmUp twice (once using "canned input" stored in a file named data.txt, and once really asking for input from the keyboard), and then runs AddEmUpGUI once. Just double-click Doit.bat to get it to run.
We'll leave the next step as "an exercise for the reader. This step is called "refactoring." The Data class needs to be refactored broken into two components one which just handles the mechanisms necessary to support that actual database, and a second which handles the command-line interface (including input and output). Then the AddEmUp applications truly will embody MVC design. Here's an abbreviated definition of refactoring from Refactoring.com:
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a 'refactoring') does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it's less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.
At this point in the design and implementation
The Data class contains the following:
Data
· dValues a data array of real values
· dAverage the average of the values currently in dValues
· nCount the number of values in dValues
Methods
· calculate() calculates the average of the values in the data array
· display() displays the contents of the data array
· getAverage() an accessor returning the value of dAverage
· getCount() an accessor returning the value of nCount
· input() gets input from the keyboard, calls insert
· insert() inserts a value into the data array
· sort() sorts the data array
The GUI class contains the following:
Data
· Variables for the components in the GUI, like labels, a text field, a button
Methods and Classes
· EnterKeyHandler handles the user pressing the Enter key; the number the user has entered is inserted into the data array
· GUI() constructor; it builds the actual graphical user interface
· QuitButtonHandler handles the user pressing the Quit button
· updateFields() resorts the data array and updates the several items that need refreshing
The CommandLine class doesn't yet exist. You'll need to create it by refactoring the Data class extracting all the I/O methods like display() and insert(). This is work for you to do.
What to do next
The next step for you is to double-click Doit.bat and let it run AddEmUp and AddEmUpGUI. Then, look over the several Java source files that comprise AddEmUp and AddEmUpGUI. The last step is to refactor the Data class, use the revised version of Data in AddEmUp and AddEmUpGUI, and check to see that both applications continue to work correctly. (Note: The batch file I've included here is named Doit.txt because Windows sometimes prevents the unzipping of "batch" files. So, rename Doit.txt to Doit.bat. Then you can double-click it.)
References
Here's a link to the AddEmUp files. The entire collection of files, including this document, is contained in AddEmUp.zip which I encourage you to download.
Here's a short collection of links that discuss MVC:
Here's a short collection of links that discuss refactoring:
Have fun!