I have a plug-in like scenario. I see how I can use the Multibinder to
collect instances of my interface from multiple modules. I'd also like
to be able to instantiate, using different modules, multiple instances
of the same concrete type instantiated on different named parameters.
So a context:
public interface Plugable {
public String supportedOperation();
public byte[] doIt(byte[] param);
}
public class PluginA implements Plugable {
public String supportedOperation() {
return "A";
}
public byte[] doIt(byte[] param) { ... }
}
public class PluginB implements Plugable {
public String supportedOperation() {
return "B";
}
public byte[] doIt(byte[] param) { ... }
}
public class DynamicPlugin implements Plugable {
private String operation;
private Object context;
@Inject
public DynamicPlugin(@Named("operation") String op, @Named
("context") Object cxt) {
this.operation = op;
this.context = cxt;
}
public String supportedOperation() {
return operation;
}
public byte[] doIt(byte[] param) {
return doStuffBasedOn(context);
}
}
public class DynamicModuleAlpha extends AbstractModule {
@Override
protected void configure() {
bind(String.class).annotatedWith(Names.named
("operation")).toInstance("C");
bind(Object.class).annotatedWith(Names.named("context")).toInstance
(//some instance);
}
}
public class DynamicModuleBeta extends AbstractModule {
@Override
protected void configure() {
bind(String.class).annotatedWith(Names.named
("operation")).toInstance("D");
bind(Object.class).annotatedWith(Names.named("context")).toInstance
(//some instance);
}
}
I understand how to use the Multibinder to have Guice make instances
of PluginA and PluginB, but I don't know how to also make instances
from DymanicModuleAlpha and DynamicModuleBeta also be created and
bound.
The Object context parameter is a little contrived. My actual case
involved xml processing with paths to different xslt files. Also,
assume I have magical code to handle conflicting plugin implementation
when there are multiple providers of the same supportedOperation().
I accept that I may be doing this wrong, but is it _way_ wrong?
--~--~---------~--~----~------------~-------~--~----~
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=en
-~----------~----~----~----~------~----~------~--~---