Use binding annotations:
http://code.google.com/p/google-guice/wiki/BindingAnnotations
If your Player class doesn't have any dependencies itself, you could
possibly just use a toInstance binding:
bind(Player.class).annotatedWith(Red.class).toInstance(new
HumanPlayer(true));
bind(Player.class).annotatedWith(Black.class).toInstance(new
HumanPlayer(false));
Then inject them:
@Inject public CheckersGame(@Red Player redPlayer, @Black Player
blackPlayer)
That's the basics. You may need something more complicated, but you should
be able to build on that.
--
Colin
On Mon, Jun 27, 2011 at 12:27 PM, blackfrancis <[email protected]> wrote:
> Hi,
> I am investigating the capabilities of Guice for inclusion in future
> projects, and playing around I've hit a small snag that I can't seem
> to find a simple answer to.
> Let's say I have a Checker's game application with a ctor like:
>
> public CheckersGame(Player redPlayer, Player blackPlayer)
>
> ...and my Player class has a ctor like:
>
> public Player(Boolean isRed)
>
> ...I've created a Module with bindings in the configure() method like:
>
> bind(CheckersGame.class).to(BasicCheckersGame.class);
> bind(Player.class).to(HumanPlayer.class);
>
> ...This will create and pass two HumanPlayers into the
> BasicCheckersGame constructor.
> How do I create bindings so that the redPlayer instance is created and
> passed to the Game ctor with a 'true' value, and blackPlayer with
> 'false'?
>
> I've seen a few examples around that seem like they might answer this,
> but they all seem quite convoluted and I'm thinking there must be a
> simple way to achieve this in the framework.
> Thanks for any info.
>
> --
> 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.
>
>
--
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.