<< 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(); }

See the full code listing at the end of this section for more information

Template method pattern: expressing invariant behavior in terms of variant behaviors

The code for the classes 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.

Caveat

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

  • the constructor JFrame and then calls
  • initialize() , which is the initialize() method of FrameAB which calls
    • super.initialize() , which should properly initialize FrameA , and then calls
    • the additional initialization code for FrameAB .

What is the chain of calls when we instantiate a FrameAC ?

Got questions? Get instant answers now!

Source code for the above frame classes

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(); }

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask