I'd like to have some default implementation of an interface, and then in a
separate module, provide a new implementation which uses the old one, via
the decorator pattern.  And everywhere that should have gotten the default,
gets the overridden one, and the modified behavior.

I can get the override to work, but getting the original implementation
injected into the override/decorator creates a circular dependency (not
surprisingly).  I thought that maybe using an annotation in the overridding
module might get around it, but then I think we'd need the annotation
*everywhere* we want that implementation, and that just gets me back to the
circular dependency.

Any ideas on how to accomplish this?

Thanks

Mike

public class TestModuleOverride {

public static void main(String[] args) {
Module base = new AbstractModule() {
@Override
protected void configure() {
bind(A.class);
}
};

Module override = new AbstractModule() {
@Override
protected void configure() {
bind(A.class).to(B.class);
}
};

Injector i = Guice.createInjector(Modules.override(base).with(override));

System.err.println(i.getInstance(A.class).doSomething());
}

public static class A {
public String toString() { return "A";}

public String doSomething() { return "A is doing something"; }
}

public static class B extends A {
private A a;
@Inject
public B(final A theA) { a = theA; }

public String toString() { return "B" + a;}

public String doSomething() { return "B is doing something, " +
a.doSomething(); }
}
}

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.

Reply via email to