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 3 of 6

The Strategy pattern

Java's Abstract Window Toolkit (AWT) provides implementations of common user interface components such as buttons, menus, scrollbars, and lists. Those components are laid out -- meaning sized and positioned -- inside containers such as panels, dialog boxes, and windows. But AWT containers do not perform the actual layout; instead, those containers delegate layout functionality to another object known as a layout manager. That delegation is an example of the Strategy design pattern.

The Strategy design pattern encapsulates a family of algorithms, or strategies, by implementing each algorithm in a different class. Clients that employ those algorithms delegate functionality to objects instantiated from those classes. That encapsulation allows clients to vary algorithms by delegating to different objects.

For the AWT, the clients are containers and the family of algorithms are layout algorithms encapsulated in layout managers. If a particular layout algorithm other than the default algorithm is required for a specific container, an appropriate layout manager is instantiated and plugged into that container. In this way, layout algorithms can vary independently from the containers that use them -- one of the hallmarks of the Strategy design pattern: The Strategy design pattern allows algorithms to vary without having to change the clients that use those algorithms.

Figure 1.a shows a simple AWT application that opens a window with six buttons. The application contains an AWT Panel contained inside an AWT Frame. The panel contains six buttons and uses its default layout manager to position and size them. The default layout manager for panels is an instance of FlowLayout, which sizes components according to their preferred sizes and positions them from left to right and top to bottom, centered in their container. You'll find the application listed in Example 1.

Figure 1. Use the default layout manager for a panel

Example 1. Use the Strategy pattern

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test {
   public static void main(String args[]) {
      Panel panel = new Panel();
      // Uncomment the following line of code to plug an 
      // instance of GridLayout into this application's panel. 
      // That layout manager will then be used to layout the 
      // application's buttons instead of the default 
      // FlowLayout layout manager.
      // panel.setLayout(new GridLayout(3, 2));
      panel.add(new Button("  1  "));
      panel.add(new Button("  2  "));
      panel.add(new Button("  3  "));
      panel.add(new Button("  4  "));
      panel.add(new Button("  5  "));
      panel.add(new Button("  6  "));
      Frame frame = new Frame("Using Layout Managers");
      frame.add(panel);
      frame.setSize(300, 150);
      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
      frame.show();
   }
}


If the commented line of code in Example 1 is uncommented, the layout manager for the application's panel will be set to an instance of GridLayout, which lays out components in a grid and sizes them equally so that the components collectively fill the container in which they reside. Figure 1b shows the result of uncommenting that line of code.

  • Print
  • Feedback

Resources