Java Reference
In-Depth Information
JFrame
JToolBar
JPanel
JComponent
Figure 2.4
The paint program is
a simple Swing application.
A static
main()
method on
PaintFrame
launches the paint program, which creates an
instance of the
PaintFrame
and each shape implementation, adding each shape
instance to the created
PaintFrame
instance. For further explanation, figure 2.5
captures the paint program classes and
their interrelationships.
To r u n t h i s n o n m o d u l a r v e r s i o n o f t h e
paint program, go into the chapter02/
paint-nonmodular/ directory of the com-
panion code. Type
ant
to build the pro-
gram, and then type
java
-jar main.jar
to run it. Feel free to click around and see
how it works; we won't go into any more
details of the program's implementation,
because
GUI
programming is beyond the
scope of this topic. The important point is to understand the structure of the program.
Using this understanding, you'll divide the program into bundles so you can enhance
and enforce its modularity.
Currently, the paint program is packaged as a single
JAR
file, which we'll call ver-
sion 1.0.0 of the program. Because everything is in a single
JAR
file, this implies that
the program isn't already modularized. Of course, single-
JAR
-file applications can still
be implemented in a modular way—just because an application is composed of multi-
ple
JAR
files, that doesn't mean it's modular. The paint program example could have
both its logical and physical modularity improved. First, we'll examine the program's
logical structure and define modules to enhance this structure. Where do you start?
One low-hanging fruit you can look for is public
API
s. It's good practice in
OSG
i
(you'll see why later) to separate your public
API
s into packages so they can be easily
shared without worrying about exposing implementation details. The paint program
has a good example of a public
API
: its
SimpleShape
interface. This interface makes it
1
Paint
frame
Simple
shape
1
1
1
Shape
component
Circle
Square
Triangle
Figure 2.5
Paint program class relationships