Monday, July 23, 2007

Java Dynamic Proxy, simple yet powerfull!

What is Proxy?

Proxy is a well know design pattern.
Proxy is an object how encapsulate another object .

Say we have some interface, and some implementations. the implementations are real subject implementations. each implementation doing concrete operation. lets say that we want to add some behavior to all implementations, without touching each implementation code. for example, we would like to write to log before and after each operation invocation. the simplest way is to use one abstract class for all implementation to extend from, and by using some template methods we can achieve this goal. but, using abstract class has its limitations:

  • if we have an object how implements two interfaces, and we want to extends some behavior for both interfaces - we cannot do that using two different abstract class.
  • each implementation should extends the abstract class,so the implementations are now "dirty" and if one of the implementations is a third party code - we cannot do that.

so, whats is the solution ?

Proxy !

We can create some new object called MyProxy.

MyProxy will implement the interface and hold a private member of interface type (called target object). the implementation of proxy will be to delegate each method to invoke the targets methods implementation, so this proxy can hold all know implementation of interface, and invoke concrete implementations.

Now, we can add behaviors like log writing, just before and after we delegate the method to target implementation.

As you can understand now, we don't have any limitations we had using abstract classes, and we are free to add any behavior we like.


What is dynamic Proxy?
Dynamic proxy is a proxy we create at runtime. no need to write a proxy object how implements the interface. we can create implementation of some interface at runtime, and encapsulate from user that he is not using his concrete implementation, and add a behaviors as we wish.

How can we do it ?
Using JDK dynamic proxy.

The object java.lang.reflect.Proxy is a dynamic proxy how can create for us proxies for a given interfaces at runtime.

first we will need to implement java.lang.reflect.InvocationHandler interface:

public MyHandler extends java.lang.reflect.InvocationHandler{

T target;

public myHandler (T target){

this.target = target.

}

Object invoke(Object proxy, Method method,Object[] args) {

//do before

method.invoke(target,args);

//do after

}

}

Now we can create proxy for any interface that we wish to add this behavior. say we want to create proxy for Foo interface encapsulating FooImpl:

Foo implementation = new FooImpl();

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), new Class[] Foo.class } , new MyHandler(implementation));

now when we execute f.someMethod() will will execute FooImpl implementation with extra behavior.

This is a simple example of using proxies to achieve extended behaviors. Proxy can be use for a lot of other use cases like security check points, lazy loading of data, and many more.

So, use it, but do not over use it!

2 comments:

Anonymous said...

viagra shelf life viagra samples viagra suppliers in the uk viagra online no prescription viagra rx cialis vs viagra how does viagra work viagra from canada get viagra viagra cheap viagra blood pressure buy viagra in canada what is generic viagra viagra 34434

Anonymous said...

top [url=http://www.001casino.com/]online casino[/url] brake the latest [url=http://www.casinolasvegass.com/]free casino[/url] unshackled no store hand-out at the foremost [url=http://www.baywatchcasino.com/]casino perk
[/url].