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

A friend of mine -- a medical doctor, no less -- once told me that he convinced a friend to take a college exam for him. Someone who takes the place of someone else is known as a proxy. Unfortunately for my friend, his proxy drank a bit too much the night before and failed the test.

In software, the Proxy design pattern proves useful in numerous contexts. For example, using the Java XML Pack, you use proxies to access Web services with JAX-RPC (Java API for XML-based remote procedure calls). Example 1 shows how a client accesses a simple Hello World Web service:

Example 1. A SOAP (Simple Object Access Protocol) proxy

public class HelloClient {
    public static void main(String[] args) {
        try {
            HelloIF_Stub proxy = (HelloIF_Stub)(new HelloWorldImpl().getHelloIF());
            proxy._setTargetEndpoint(args[0]);
            System.out.println(proxy.sayHello("Duke!"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


Example 1's code closely resembles the Hello World Web services example included with JAX-RPC. The client obtains a reference to the proxy, and sets the proxy's endpoint (the Web service's URL) with a command line argument. Once the client has a reference to the proxy, it invokes the proxy's sayHello() method. The proxy forwards that method call to the Web service, which often resides on a different machine than that of the client.

Example 1 illustrates one use for the Proxy design pattern: accessing remote objects. Proxies also prove useful for creating expensive resources on demand, a virtual proxy, and for controlling access to objects, a protection proxy.

If you've read my "Decorate Your Java Code" (JavaWorld, December 2001), you may see similarities between the Decorator and Proxy design patterns. Both patterns use a proxy that forwards method calls to another object, known as the real subject. The difference is that, with the Proxy pattern, the relationship between a proxy and the real subject is typically set at compile time, whereas decorators can be recursively constructed at runtime. But I'm getting ahead of myself.

In this article, I first introduce the Proxy pattern, starting with a proxy example for Swing icons. I conclude with a look at the JDK's built-in support for the Proxy pattern.

Note: In the first two installments of this column -- "Amaze Your Developer Friends with Design Patterns" (October 2001) and "Decorate Your Java Code" -- I discussed the Decorator pattern, which closely relates to the Proxy pattern, so you may wish to look at these articles before proceeding.

The Proxy pattern

Proxy: Control access to an object with a proxy (also known as a surrogate or placeholder).

Swing icons, for reasons discussed in the "Proxy Applicability" section below, represent an excellent choice for illustrating the Proxy pattern. I begin with a short introduction to Swing icons, followed by a discussion of a Swing icon proxy.

Swing icons

Swing icons are small pictures used in buttons, menus, and toolbars. You can also use Swing icons by themselves, as Figure 1 illustrates.

  • Print
  • Feedback

Resources