1 / 7

Dynamic Proxy

Dynamic Proxy. Proxy: Addition to the Java 1.3 reflection package: implements a list of interfaces specified at runtime can be used as if the Proxy class implemented these interfaces! when invoked passes the “method invocation” to an implementation of the InvocationHandler

probst
Download Presentation

Dynamic Proxy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Proxy • Proxy: Addition to the Java 1.3 reflection package: • implements a list of interfaces specified at runtime • can be used as if the Proxy class implemented these interfaces! • when invoked passes the “method invocation” to an implementation of the InvocationHandler • how does this affect the stub-skeleton design? Good Java Programming

  2. Dynamic Proxy: Instantiation InvocationHandler handler = new GenericSerializationHandler(); Foo1 proxyInstance = (Foo1) Proxy.newProxyInstance( Foo.class.getClassLoader(), new class[ ] { Foo.class, Foo1.class}, handler); Good Java Programming

  3. Dynamic Proxy: Invocation // invocation examples ((Foo)proxyInstance).bar(); ((Foo1)proxyInstance).bar1(); Good Java Programming

  4. Dynamic Proxy: Invocation Handler public GenericSerializationHandler implements InvocationHandler { … public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { String methodName = m.getName(); Class className = m.getDeclaringClass(); if (className.equals(Foo.class) && methodName.equals(“bar”)) { System.out.print(“Method bar called”); } } // className: interface on which the method was invoked // proxy: proxy instance on which the method was invoked // args: value of arguments in the methods Good Java Programming

  5. Proxy Pattern • Provides a surrogate or placeholder for another object to control access to it. Good Java Programming

  6. Proxy Pattern: Variations • Remote Proxy: controls access to a remote object • proxy acts as a local representative for an object in a different JVM • Virtual Proxy: controls access to a resource that is expensive to create • Defers creation of the object until needed • Once the object is created, proxy delegates requests to the real object • Caching Proxy • maintains a cache of previously created objects and returns cached objects whenever appropriate • Protection Proxy: controls access to a resource based on access rights • How can this be implemented using Dynamic Proxy in Java? Good Java Programming

  7. Virtual Proxy • Defers creation of the object until needed • Once the object is created, proxy delegates requests to the real object • Example: • Creates a generic icon for an image, while waiting for the image to be retrieved from the network • Once the image is received, replaces the generic one with the actual image Good Java Programming

More Related