Java Reference
In-Depth Information
Creating a different configuration of the paint program is as simple as creating a
new static main() method for the launcher to invoke. Currently, you're using the orig-
inal static main() method provided by PaintFrame . In truth, it isn't modular to have the
static main() on the implementation class; it's better to create a separate class so you
don't need to recompile the implementation classes when you want to change the
application's configuration. The following listing shows the existing static main()
method from the PaintFrame class.
Listing 2.2 Existing PaintFrame.main() method implementation
pcublic class PaintFrame extends JFrame
implements MouseListener, MouseMotionListener {
...
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
PaintFrame frame = new PaintFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) ;
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
frame.addShape(new Circle());
frame.addShape(new Square());
frame.addShape(new Triangle());
frame.setVisible(true);
}
});
}
The existing static main() is simple. You create a PaintFrame instance B and add a lis-
tener C to cause the VM to exit when the PaintFrame window is closed. You inject the
various shape implementations into the paint frame D and make the application win-
dow visible. The important aspect from the point of view of modularity is at D .
Because the configuration decision of which shapes to inject is hardcoded into the
method, if you want to create a different configuration, you must recompile the
implementation bundle.
For example, assume you want to run the paint program on a small device only
capable of supporting a single shape. To do so, you could modify Paint-
Frame.main() to only inject a single shape, but this wouldn't be sufficient. You'd also
need to modify the metadata for the bundle so it would no longer depend on the
other shapes. Of course, after making these changes, you'd lose the first configura-
tion. These types of issues are arguments why the static main() method should be in
a separate bundle.
Let's correct this situation in the current implementation. First, delete the Paint-
Frame.main() method and modify its bundle metadata as follows:
B
Creates PaintFrame
instance
Adds
listener
C
D
Injects shape
implementations
 
Search WWH ::




Custom Search