Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Amaze your developer friends with design patterns

<strong>Java Design Patterns</strong> column kicks off with a look at three important design patterns

  • Print
  • Feedback

Page 4 of 6

Figure 1b. Use the Strategy pattern to change layout

To modify its layout algorithm, notice that you need not make code changes to an AWT container class, such as Panel. That's because the Strategy pattern encapsulates the concept that varies -- in this case layout algorithms -- which is one of the fundamental tenets of object-oriented design, and is a recurring theme among design patterns.

The Composite pattern

As mentioned above, the AWT implements components and containers. Components can be added to a container, as illustrated by the code listed in Example 1, by using the Component add(Component) method defined in java.awt.Container.

To facilitate complex user interface screens, user interface toolkits must allow nested containers, effectively composing components and containers into a tree structure. Additionally, it's crucial for components and containers in that tree structure to be treated uniformly, without having to distinguish between them. For example, when the AWT determines the preferred size of a complex layout containing nested components and containers, it walks the tree structure and asks each component and container for its preferred size. If that traversal of the tree structure required distinction between components and containers, it would unnecessarily complicate that code, making it harder to understand, modify, extend, and maintain.

The AWT accomplishes nesting containers and uniform treatment of components and containers by implementing the Composite pattern. The Composite pattern dictates that containers are components, typically with an abstract class that represents both. In the AWT, that abstract class is java.awt.Component, the superclass of java.awt.Container; therefore, an AWT container can be passed to the add(Component) method from java.awt.Container because containers are components.

Figure 2 shows an applet that, by nesting containers, takes advantage of the AWT's Composite pattern implementation. (Note: If you download the code to this article, use the JDK's appletviewer to test the applet listed in Example 2 to avoid incompatibilities in your browser's JVM.)

Figure 2. Use the Composite pattern to nest AWT containers

The applet shown in Figure 2 is listed in Example 2:

Example 2. Use the Composite pattern

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.Label;
import java.awt.TextField;
public class Test extends Applet {
   public void init() {
      Panel        center = new Panel();
      WorkPanel workPanel = new WorkPanel(center);
      workPanel.addButton("Ok");
      workPanel.addButton("Cancel");
      center.add(new Label("Name:"));
      center.add(new TextField(25));
      setLayout(new BorderLayout());
      add(workPanel);
   }
}
class WorkPanel extends Panel {
   private Panel buttonPanel = new Panel();
   public WorkPanel(Panel centerPanel) {
      setLayout(new BorderLayout());
      add(centerPanel, "Center");
      add(buttonPanel, "South");
   }
   public void addButton(String label) {
      buttonPanel.add(new Button(label));
   }
}


The applet listed in Example 2 contains a panel -- an instance of WorkPanel -- that contains two other panels, as shown in Figure 3.

  • Print
  • Feedback

Resources