Wednesday, May 6, 2015

Proxy pattern - structural design pattern

The proxy pattern is a structural design pattern. In the proxy pattern, a proxy class is used to control access to another class. The reasons for this control can vary. As one example, 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. Another reason to use a proxy is to control access rights to an object. A client request may require certain credentials in order to access the object.
Now, we'll look at an example of the 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

package com.cakes;

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

package com.cakes;

public class FastThing extends Thing {

 public FastThing() {
 }

}
SlowThing also subclasses Thing. However, its constructor takes 5 seconds to execute.

SlowThing.java

package com.cakes;

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

package com.cakes;

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

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
Source : http://www.avajava.com/tutorials/lessons/proxy-pattern.html

No comments :

Post a Comment