<< Chapter < Page | Chapter >> Page > |
public AFrame(String title) {
// Always call the superclass's constructor:super(title);
addWindowListener(new java.awt.event.WindowAdapter() {public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);}
});initialize();
}
AFrame
,
FrameA
,
FrameAB
,
and
FrameAC
are available at the end of this section.The code for
AFrame
is an example of what is called the
Template Method Pattern . This design pattern is used to expresses an
invariant and concrete behavior that consists of calls to one or more
abstract methods. An abstract method represents a
variant behavior. In other words, the template design pattern is a means to express an invariant behavior in terms of variant behaviors. We shall see this pattern used again in expressing sorting algorithms.
As stated earlier, the call to
initialize()
in the constructor of
AFrame
will only invoke the concrete
initialize()
method of the concrete descendant class of
AFrame
that is being instantiated. Because
initialize()
is polymorphic, care must taken to ensure proper initialization of descendant classes that are more than one level deep in the inheritance hierarchy of
AFrame
. Consider the following inheritance tree as illustrated by the following UML class diagram.
In overriding the
initialize()
method of
FrameA
, a direct subclass of
AFrame
, we should not invoke the
initialize()
method of the superclass
AFrame
via the call
super.initialize()
because
super.initialize()
is abstract. However, the
initialize()
method of any subclass of
FrameA
and below should make a call to the initialize() method of its direct superclass in order to ensure proper initialization. For example, in the above diagram,
new FrameAB("ab")
calls the constructor
FrameAB
, which calls the constructor
FrameA
, which calls the constructor
AFrame
, which calls
JFrame
and then callsinitialize()
, which is the
initialize()
method of
FrameAB
which calls
super.initialize()
, which should properly initialize
FrameA
, and then callsFrameAB
.What is the chain of calls when we instantiate a
FrameAC
?
Aframe.java
package view;
import javax.swing.*; // to use JFrame./**
* Minimal reusable code to create a JFrame with a title and a window event* listener to call on System.exit() and force termination of the main
* application that creates this JFrame.* @author D.X. Nguyen
*/public abstract class AFrame extends JFrame {
public AFrame(String title) {// Always call the superclass's constuctor:
super(title);/**
* Add an anonymous WindowAdapter event handler to call System.exit to* force termination of the main application when the Frame closes.
* Without it, the main application will still be running even after* the frame is closed.
* For illustration purpose, we use the full package name for* WindowAdpater and WindowEvent.
* We could have imported java.awt.event.* and avoid using the full* package names.
*/addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {System.out.println(e); // For illustration purpose only.
System.exit(0);}
});/**
* Subclasses are to do whatever is necessary to initialize the frame.* CAVEAT: At run-time, when a concrete descendant class of AFrame is
* created, only the (concrete) initialize() method of this descendant* class is called.
*/initialize();
}/**
* Relegates to subclasses the responsibility to initialize the system to* a well-defined state.
*/protected abstract void initialize();
}
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?