Parameters in Provider.

129 views
Skip to first unread message

ale

unread,
Apr 21, 2009, 11:32:15 AM4/21/09
to google-guice
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

unread,
Apr 21, 2009, 12:38:29 PM4/21/09
to google...@googlegroups.com
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 <ale.s...@gmail.com>



--
Olivier Grégoire
Boulevard De Smet De Naeyer 627A
1020 Bruxelles
Tél: +32.486.74.09.49

ale

unread,
Apr 21, 2009, 2:35:53 PM4/21/09
to google-guice
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 <ogrego...@gmail.com> 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 <ale.sch...@gmail.com>

ale

unread,
Apr 21, 2009, 4:13:41 PM4/21/09
to google-guice
i can use something like this. but if i have 25 commands ?, i have to
pass 25 parameters in the constructor. ?

public class CommandProviderFactory {
private Provider<CommandX> commandXProvider;
private Provider<CommandY> commandYProvider;

@Inject public CommandProviderFactory(Provider<CommandX>
commandXProvider,
Provider<CommandY> commandYProvider) {
}

public Command create(String commandName) throws Exception {
if(commandName.equals("commandX"))
return commandXProvider.get();
else if(commandName.equals("commandY"))
return commandYProvider.get();
else
throw new Exception("command not found");

Alen Vrecko

unread,
Apr 21, 2009, 6:00:03 PM4/21/09
to google-guice
I just look at this briefly maybe a long-shot but it can't hurt. Maybe
this code will give you some idea. It uses Guice 2.

http://pastie.org/453944

Cheers,
Alen

ale

unread,
Apr 21, 2009, 9:27:49 PM4/21/09
to google-guice
that is incredible. you hit the bull's-eye. (is that the expression?).
i am learning english with my sister.

that is just what i needed.

many thanks.
Reply all
Reply to author
Forward
0 new messages