Thanks Oliver.
but i don't understand how assistedinject can help me.
can you help me fix this example code.
/* Main */
public class Main {
public static class MainModule extends AbstractModule {
@Override protected void configure() {
bind(RemoteServiceX.class).to(RemoteServiceXImpl.class);
bind(RemoteServiceY.class).to(RemoteServiceYImpl.class);
}
}
public static void main(String[] args) {
Injector inejector = Guice.createInjector(new MainModule());
Test test = inejector.getInstance(Test.class);
Command command = test.processRequest("commandX");
command.execute();
}
}
public class Test {
public Command processRequest(String commandName) {
if( commandName.equals("commandX") )
return new CommandX(?); //HERE IS THE PROBLEM
else if ( commandName.equals("commandX") )
return new CommandY(?); //HERE IS THE PROBLEM
return null;
}
}
/* Command */
public interface Command {
void execute();
}
public class CommandX implements Command {
private RemoteServiceX service;
private String runtimeParameter;
@Inject public CommandX(RemoteServiceX service, String
runtimeParameter) {
this.service = service;
this.runtimeParameter = runtimeParameter;
}
@Override public void execute() { /*TODO: implements this method*/ }
}
public class CommandY implements Command{
private RemoteServiceX service;
private String runtimeParameter;
@Inject public CommandY(RemoteServiceX service, String
runtimeParameter) {
this.service = service;
this.runtimeParameter = runtimeParameter;
}
@Override public void execute() { }
}
/* Services */
public interface RemoteServiceX {
public void specificMethod1OfServiceX();
public void specificMethod2OfServiceX();
}
public class RemoteServiceXImpl implements RemoteServiceX {
@Override public void specificMethod1OfServiceX() {/*TODO: implements
this method*/}
@Override public void specificMethod2OfServiceX() {/*TODO: implements
this method*/}
}
public interface RemoteServiceY {
public void specificMethod1OfServiceY();
public void specificMethod2OfServiceY();
}
public class RemoteServiceYImpl implements RemoteServiceY {
@Override public void specificMethod1OfServiceY() {/*TODO: implements
this method*/}
@Override public void specificMethod2OfServiceY() {/*TODO: implements
this method*/}
}
On Apr 21, 1:38 pm, Olivier Grégoire <[email protected]> wrote:
> Hello,
>
> You must create a factory instead of using a Provider. To help you, the
> extension AssistedInject exists.
>
> More details in the
> wiki:http://code.google.com/p/google-guice/wiki/AssistedInject
>
> Regards,
> Olivier
>
> 2009/4/21 ale <[email protected]>
>
>
>
>
>
> > How can i do this.?
>
> > public class CommandProvider implements Provider<Command> {
>
> > public Command get(runtimeValue1, runtimeValue2) {
>
> > if(runtimeValue1.equals( "commandX" )) {
>
> > return new FooCommandX( runtimeValue2 );
>
> > } else if (runtimeValue1.equals( "commandY" )) {
>
> > return new FooCommandY(runtimeValue2);
>
> > } else if (runtimeValue1.equals( "commandZ" )) {
>
> > return new FooCommandZ(runtimeValue2);
>
> > } else {
> > throw new RuntimeException("...."); //For this i
> > know that i should use ThrowingProviders.
> > }
>
> > }
>
> > }
>
> > Thanks.
>
> --
> Olivier Grégoire
> Boulevard De Smet De Naeyer 627A
> 1020 Bruxelles
> Tél: +32.486.74.09.49
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---