Proxy design pattern
The proxy design pattern allows you to provide an interface to other objects by creating a wrapper class as the proxy. The wrapper class, which is the proxy, can add additional functionality to the object of interest without changing the object’s code. Here Proxy means ‘’ on behalf of’. that directly explains proxy design pattern. It is one of the simplest and straight forward design pattern. To put it in even more simplest form it is like: in attendance we give proxy for our friend in college.
As you can see it’s quite simple – the Proxy is providing a barrier between the user and the real application.
In the proxy pattern, a proxy class is used to control access to another class. a proxy may avoid instantiation of an object until the object is needed. This can be useful if the object requires a lot of time or resources to create.
Now let’s see an example of how to implement proxy pattern. First, we’ll create an abstract class called Thing with a basic sayHello() message that includes the date/time that the message is displayed.
Thing.java
1 2 3 4 5 6 7 8 9 |
package com.jrc; import java.util.Date; public abstract class Thing { public void sayHello() { System.out.println(this.getClass().getSimpleName() + " says howdy at " + new Date()); } } |
FastThing subclasses Thing.
FastThing.java
1 2 3 4 5 6 |
package com.jrc; public class FastThing extends Thing { public FastThing() { } } |
SlowThing also subclasses Thing. However, its constructor takes 5 seconds to execute.
SlowThing.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.jrc; public class SlowThing extends Thing { public SlowThing() { try { Thread.sleep(5000); }catch (InterruptedException e) { e.printStackTrace(); } } } |
The Proxy class is a proxy to a SlowThing object. Since a SlowThing object takes 5 seconds to create, we’ll use a proxy to a SlowThing so that a SlowThing object is only created on demand. This occurs when the proxy’s sayHello() method is executed. It instantiates a SlowThing object if it doesn’t already exist and then calls sayHello() on the SlowThing object.
Proxy.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.jrc; import java.util.Date; public class Proxy { SlowThing slowThing; public Proxy() { System.out.println("Creating proxy at " + new Date()); } public void sayHello() { if (slowThing == null) { slowThing = new SlowThing(); } slowThing.sayHello(); } } |
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
1 2 3 4 5 6 7 8 9 10 11 |
package com.jrc; 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:
1 2 3 |
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 |
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.
The cases can be innumerable why we can use the proxy. Let’ say we need to withdraw money to make some purchase. The way we will do it is, go to an ATM and get the money, or purchase straight with a cheque. In old days when ATMs and cheques were not available, you used to get your passbook, go to bank, get withdrawal form there, stand in a queue and withdraw money. Then go to the shop where you want to make the purchase. In this way, we can say that ATM or cheque in modern times act as proxies to the Bank.
Leave a Reply