| Understanding Dynamic Proxies |
| Java Concepts | ||||||
| Written by administrator | ||||||
| Friday, 02 May 2008 06:17 | ||||||
|
Applications Remote Proxy: - When you want your application to interact with another application residing on some other machine on the network or internet. Generic Listeners: - You will be familiar with event listeners in swings. Suppose you do not know the UI components that will be present in your application when coding (which means that the components will be created using a factory or using reflection), then you cannot code the listeners of those components and using a simple factory to create listeners is not a good idea because you also don’t know what all interfaces are to be implemented by one listener. In this situation dynamic proxies come to your help. Well, I could think of only these many applications, will update when I get more applications of Dynamic Proxies.
Class you should get to Know: 1) java.lang.reflect.InvocationHandler – When you create proxy objects you would want come code to be executed when any method is called on the Proxy Objects, you write that code to be executed in your implementation of this InvocationHandler Interface. The Invocation Handler interface has only one method:- Object invoke(Object proxy, Method method, Object[] args) Here you can see that There are three arguments passed to this method, lets see what they mean:- a) The proxy Object – This is nothing but the object on which a method was called which caused this method on the invocation handler to the invoked. You might think ‘why this argument is needed?’ Well, you can use one invocation handler to create more that one proxy which implements different interfaces in which case you would want to know the object on which the method was called. b) The Method - The method in the proxy Object which was called. Its trivial why this is needed. c) args Object array - The arguments that were passed to the method that was invoked. 2) java.lang.reflect.Proxy – User to create Proxy Objects which will be implementing all the interfaces that you desire. To create a new Proxy object, you use the static method static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler handler) Lets see what the arguments mean:- a) Loader :- it is the class loader to be used to hold the Dynamic Class that was created. The dynamic class that is created will be extending the Proxy class and implementing the interfaces given by you and the proxy object that is returned will be an instance of this class. b) Interfaces:- is the list of interfaces that this proxy object should implement c) Handler:- is the handler which should be invoked when any method is invoked on the proxy object.
Example To learn to use dynamic proxies lets develop a small application where we shall be using dynamic proxy to create a listener that implements MouseListener and MouseMotionListener and then use this listener instance to print all the mouse events that occur on a JFrame.
First let’s create a class called TestHandler that implements InvocationHandler. { public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { System.out.println("Invocation handler invoke method " + method.getName() + " with args " + args.toString()); return null; } } In the invoke Method, we are just printing the method name and the arguments
Now lets write our main function.
public static void main(String[] args) { JFrame frame = new Testframe(); Object proxy = Proxy.newProxyInstance(frame.getClass().getClassLoader(), new Class[] { MouseListener.class, MouseMotionListener.class },new TestHandler()); frame.addMouseListener((MouseListener) proxy); frame.addMouseMotionListener((MouseMotionListener) proxy); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setVisible(true);
} Lets now inspect the snippet of code that create a proxy instance Object proxy = Proxy.newProxyInstance(frame.getClass().getClassLoader(), new Class[] { MouseListener.class, MouseMotionListener.class },new TestHandler());
We are creating a new proxy instance for a dynamic class that implements MouseListener and MouseMotionListener . All the method calls in this object will be delegated to the instance of TestHandler that is passed.
Program Output: Invocation handler invoke method mouseEntered with args [Ljava.lang.Object;@1ffb8dc
Only registered users can write comments!
Powered by !JoomlaComment 3.22
3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
||||||
| Last Updated ( Friday, 02 May 2008 06:36 ) | ||||||








