I’m trying to make a facade for a library I’m giving out. In my facade I use Guice to contruct the object graph. Deep in the object graph is a Proxy object, that has getURL/setURL methods.
In my facade, how do I get the Proxy instance used to create my root object? I want my facade to have url getter and setter.
I tried something like this:
public class SomeThingFacade() {
private final SomeThing thing;
private final HTTPProxy proxy;
public SomeThingFacade() {
MyModule module = new MyModule();
Injector injector = Guice.createInjector(module);
// this is the main class I'm making a facade for
this.thing = injector.getInstance(SomeThing.class);
// deep in the "thing" object graph is a Proxy implementation
this.proxy = injector.getInstance(HTTPProxy.class);
}
public void setURL(URL url) {
this.proxy.setURL(url);
}
}
but the injector.getInstance created a new instance.
Binding in MyModule:
bind(Proxy.class).to(HTTPProxy.class).asEagerSingleton();
I had previously hardcoded the object graph in the facade constructor, but it got unwieldly with 30 objects.
Basically, I need to configure an instance deep in the object graph after creation, but I’m not sure how I get a hold of that instance.
This totally looks like a good serious question. However, I can’t figure out what’s being asked exactly.
My answer, looking at the code and ignoring the fascaded object graph talk (so let me know if I misunderstood you completely), is:
If
thing‘sSomeThingdepends somewhere on adeep internal proxy, the module should configure it to be bound toHTTPProxyfor thing. The secondgetInstancedoes not affect the first. The only way you could somehow be doing something that makesproxyaffectthingis if HTTPProxy were boundin(Singleton.class), then by calling methods on proxy that affect the members and behavior ofHTTPProxywhich would also be the same instance deep insidethingyou might be doing what you’re looking for. I don’t see why you’d want to do it this way though. Consider instead writing a provider that configuresHTTPProxyand/or making a special module just for the facade’s use.