Proxy Pattern
Author: Deron Eriksson
Description: This Java tutorial describes the proxy pattern, a structural design pattern.
Tutorial created using: Windows Vista || JDK 1.6.0_11 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)


Page: < 1 2

(Continued from page 1)

The ProxyDemo class demonstrates the use of our proxy. It creates a Proxy object and then creates a FastThing object and calls sayHello() on the FastThing object. Next, it calls sayHello() on the Proxy object.

ProxyDemo.java

package com.cakes;

public class ProxyDemo {

	public static void main(String[] args) {

		Proxy proxy = new Proxy();

		FastThing fastThing = new FastThing();
		fastThing.sayHello();

		proxy.sayHello();

	}

}

The console output of executing ProxyDemo is shown here. From the output, notice that creating the Proxy object and calling sayHello() on the FastThing object occurred at 16:41:06, and calling sayHello() on the Proxy object did not call sayHello() on the SlowThing object until 16:41:11. We can see that the SlowThing creation was a time-consuming process. However, this did not slow down the execution of our application until the SlowThing object was actually required. We can see here that the proxy pattern avoids the creation of time-consuming objects until they are actually needed.

Console Output

Creating proxy at Sat May 03 16:41:06 PDT 2008
FastThing says howdy at Sat May 03 16:41:06 PDT 2008
SlowThing says howdy at Sat May 03 16:41:11 PDT 2008
Page: < 1 2


Related Tutorials: