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

Take control with the Proxy design pattern

The Proxy design pattern substitutes a proxy for an object, making your apps more efficient

  • Print
  • Feedback

Page 5 of 6

Proxy applicability

The Proxy pattern applies whenever you need to control access to an object. The most common situations include:

  • Remote proxies
  • Virtual proxies
  • Protection proxies


Remote proxies control access to remote objects, such as the Web service proxy discussed at the beginning of this article. Virtual proxies -- the image-icon proxy is an example -- control access to resources that are expensive to create, such as large images. Protection proxies control what functionality specific users can access.

Classes that implement stable interfaces with few methods are the best candidates for a proxy's real subject because their proxies are easy to implement and maintain. For example, the Icon interface defines just three methods, unchanged since Swing's 1998 release. Writing an image-icon proxy is a fairly simple matter, but, for example, creating a proxy whose real subject is a Swing component proves much harder because you literally must implement hundreds of forwarding methods. Of course, the JDK's built-in Proxy pattern support makes it much easier to implement proxies whose real subjects have a large number of methods.

Finally, let's look quickly at the difference between the Decorator and Proxy patterns. Although you implement both patterns in an almost identical fashion, it's the intent of those patterns that differs. The Decorator pattern constructs objects at runtime by recursively enclosing an object within one or more decorators. The Proxy pattern acts as a stand-in for a real subject, which is set at compile time.

Homework

For this month's homework assignment, reimplement the TableBubbleSortDecorator discussed in "Decorate Your Java Code" with the JDK's built-in Proxy pattern support.

Homework from last time

Last month's homework asked you to implement a filter decorator -- extending the TableFilterDecorator class shown in Figure 8 from "Decorate Your Java Code" -- that filters high-priced items in the table shown in that article's Figure 6.

The filter I implemented for this assignment filters items that cost more than one dollar. Figure 1H shows the application discussed last time fitted with a button that applies the filter to the application's table. Figure 2H shows the application after the filter button has activated.

Figure 1H. Before filtering

Figure 2H. After filtering

The application shown in Figure 1H and Figure 2H is partially listed in Example 1H:

Example 1H. A filter decorator

...
public class Test extends JFrame {
   public static void main(String args[]) {
      SwingApp.launch(new Test(), "A Sort Decorator", 
                                   300, 300, 450, 250);
   }
   public Test() {
      // Create the decorator that will decorate the table's 
      // original model. The reference must be final because it's
      // accessed by an inner class below. Notice that the 
      // reference is TableSortDecorator even though it
      // points to a TableBubbleSortDecorator--that
      // way, the mouse listener below will work with any type
      // of TableSortDecorator.
      final TableSortDecorator sortDecorator = 
            new TableBubbleSortDecorator(table.getModel());
      final TableFilterDecorator filterDecorator =
            new TableHighPriceFilter(sortDecorator);
      // Set the table's model to the decorator. Because the
      // decorator implements TableModel, the table will never
      // know the difference between the decorator and its
      // original model.
      table.setModel(filterDecorator);
      ...
      filterButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            filterDecorator.filter(PRICE_COLUMN);
            repaint();
         }
      });
   }
   ...
}


The application shown above wraps the table's original model with a sort decorator, which is subsequently wrapped with a filter decorator. For this homework assignment, our focus is the filter decorator.

  • Print
  • Feedback

Resources