Dynamic Proxy


J2SE version 1.3 introduced a new class, java.lang.reflect.Proxy, that allows you to construct dynamic proxy classes. A dynamic proxy class allows you to dynamicallyat runtimeimplement one or more interfaces.

The proxy pattern is one of the more useful patterns identified in the book Design Patterns.[10] A proxy is a stand-in; a proxy object stands in for a real object. In a proxy pattern implementation, client objects think they are interacting with the real object but are actually interacting with a proxy.

[10] [Gamma1995].

Figure 12.3 shows the proxy pattern in the context of distributed object communication. A ServiceImplementation class resides on a separate machine in a separate process space from the Client class. In order for a Client to interact with a ServiceImplementation, some low-level communications must take place. Suppose Client must send the message submitOrder to a ServiceImplementation. This Java message send must be translated to a data stream that can be transmitted over the wire. On the server side, the data stream must be reconstituted into a Java message to be sent to the ServiceImplementation object.

Figure 12.3. Distributed Communication Using a Proxy


You want the code in Client to be able to work with the code in ServiceImplementation as if it were executing in the same local Java VM process. Neither the code in Client nor the code in ServiceImplementation should have to be concerned with the nuts and bolts of the remote communication.

The solution is to have the ServiceImplementation class implement a Service interface that a client-side class, ClientSkeletonProxy, also implements. A Client object can then interact with a ClientSkeletonProxy through this interface, thinking that it is interacting with the real ServiceImplementation. Thus, the ClientSkeletonProxy is a stand-in, or proxy, for the real ServiceImplementation. The ClientSkeletonProxy takes incoming requests and collaborates with a ServerStub on the remote machine to do the low-level communication. The ServerStub takes incoming data transmissions and translates them into calls against the ServiceImplementation. (You might recognize that implementing this solution in Java is heavily dependent upon the use of reflection.)

In Java, this proxy scheme is the basis of a technology known as Remote Method Invocation (RMI). RMI, in turn, is the basis of the Enterprise Java Beans (EJB) technology for component-based distributed computing.[11]

[11] EJB is a part of the J2EE (Java 2 Enterprise Edition) platform.

Proxies have many other uses, including techniques known as lazy load, copy on write, pooling, caching, and transactional markup. You will code a transparent security mechanism using a proxy.

In a Java proxy implementation, the proxy implements the same interface as the real object. The proxy intercepts messages sent to a client; after processing a message, the proxy often delegates it to the real, or target, object. You can imagine that maintaining proxies can become tedious if a proxy class must implement the same interface as the target class. For every method you add to an interface, you must provide both the real implementation and the proxy implementation. Fortunately, the dynamic proxy in Java eliminates the need for you to explicitly implement every interface method within the proxy class.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net