[previous] [programme] [next]

 TU1 

   

OT2002 Session

Using Java's Dynamic Proxy Classes

   

How Dynamic Proxies can be used to create flexible software

Monday 8 April, 11:45--13:00

tutorial -    75 minutes

Benedict Heal


 
Abstract
Java 1.3 introduced the Dynamic Proxy facility, which offers one of the biggest changes in Java programming style since the introduction of Reflection. A proxy is an object which stands between a client and a real object. Typically it filters or modifies messages before forwarding them to the real object. For example, a tracing proxy might log all messages before forwarding them, or a checking proxy might check message parameters before forwarding. Usually a proxy class has to be written by hand for each class of real object. The decision about whether or not a given class has a tracing proxy is thus a compile-time decision.

The new Dynamic Proxy facility lets one create proxy classes on the fly. Given a set of interfaces, one can at runtime create a new object which supports each of the interfaces. When the proxy receives any message, it passes it on to an ‘InvocationHandler??’, which can therefore treat all messages uniformly. We can thus at runtime bolt on behaviour like message tracing to arbitrary objects. We don’t have to decide at compile time which classes shall have it or not have it.

This opens up all sorts of new programming techniques. We can assemble and dynamically re-configure chunks of behaviour at runtime, instead of fixing it all at compile time. We can even construct ‘proxies’ which don’t have a real object behind them, enabling us with minimal programming to create ‘mock’ objects for testing purposes.

Though Dynamic Proxies are usually presented as of interest only to systems programmers, they offer a variety of idioms which can be of general interest. They help move Java from the statically typed mindset of C++ towards far more dynamic program structures of Smalltalk and LISP.

The tutorial will briefly review the reflection facilities of Java, and then present the Dynamic Proxy mechanism. A variety of idioms using it to solve a variety of problems will be presented. By standing back and considering the facilities abstractly, the session will end with some brainstorming in groups to search for more applications of the techniques introduced.

Audience
Competent Java developers, preferably with some knowledge of reflection
Benefits
* understand the Java dynamic proxy facilities * see how they relate to the decorator and proxy design patterns, and to mock objects * understand the limitations the Java language places on more general programming structures.

Materials
Copies of slides Handout to focus brain-storming for new applications

 


Benedict Heal

Independent
I'm an independent consultant/trainer keen on teaching OOAD via Catalysis/UML as well as Java techniques. I particularly enjoy mentoring projects new to OO.

I am fascinated at how with reflection and dynamic proxy classes Java is evolving from a statically typed language into something with the fluidity and danger of Smalltalk, and am keen to help Java practicing users learn some of these more advanced techniques.


[previous] [programme] [next]