> @Brian:
> 
> Can you give me an example of using dynamic proxies, as you explained?

You create a dynamic proxy that implements your interface and then the 
MethodHandler does a lookup based on the parameters passed in. Looks roughly 
like:

Service s = Proxy.newProxy(Service.class, new MyHandler());
bind(Service.class).toInstance(s);

class MyHandler implements MethodHandler {
  Object call(Object[] params) {
    if (params[0].equals("foo")) {
      Service realService = ...; // Do lookup here
      return realService.doSomething((String) params[0]);
    }
    ...
  }
}

This uses the parameters passed in to the service to do the lookup and find the 
correct implementation to use. It then invokes the real service and passes the 
parameters along. 

I like this pattern and it could be wired into Guice. Be nice to do something 
like:

bind(Service.class).toProxy(MyServiceProxy.class);

Service s = Injector.newInstance(Service.class);
s.doSomething(42);

class MyServiceProxy implements Proxy<Service> {
  @Inject FourtyTwoService fourtyTwoService;
   ... // Other implementations could be injected here

  Service lookup(Object[] args) {
    // Do lookup here using arguments passed to the service
    int number = (int) args[0];
    if (number == 42) {
      return fourtyTwoService;
    }
    ...
  }
}

Guice would use the proxy to find the real instance and then invoke the real 
instance instead. This is essentially the same as above, but everything is 
wired together and handled by Guice.

-bp

--

You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=.


Reply via email to