Suggestion for solving the "robot legs" problem

58 views
Skip to first unread message

Esko Luontola

unread,
Jan 14, 2009, 2:52:38 PM1/14/09
to google-guice
Would it be possible to solve the "robot legs" problem like this?

bind(Leg.class).annotatedWith(Left.class).to
(Leg.class).injecting(LeftFoot.class);
bind(Leg.class).annotatedWith(Right.class).to
(Leg.class).injecting(RightFoot.class);

The Leg class has a constructor parameter of type Foot and possibly
also other parameters. The "injecting" method is used to define the
Foot implementation that will be injected to Leg.

Here are some issues that will still need to be thought about:

- It should be possible to specify multiple explicit dependencies, for
example "injecting(LeftFoot.class, LeftKnee.class)" or "injecting
(LeftFoot.class).injecting(LeftKnee.class)". In the latter case it
would be possible to also specify the dependencies as object instances
or providers, by having overloaded "injecting" methods or
"injectingInstance", "injectingProvider" etc. methods.

- How to handle the cases where Foot is not a direct dependency of
Leg, but instead the dependency of one of Leg's dependencies?

- What would be the best name for the "injecting" method? One other
possibility is "usingDependencies".

Robbie Vanbrabant

unread,
Jan 14, 2009, 3:26:15 PM1/14/09
to google...@googlegroups.com

Esko Luontola

unread,
Jan 14, 2009, 5:55:46 PM1/14/09
to google-guice
I know about private modules. I just thought that it would be nicer to
do it with less code.

tzwoenn

unread,
Jan 15, 2009, 8:17:27 AM1/15/09
to google-guice
There is actual no binding for Foot.class to LeftFoot.class, so I ask
myself how should guice figure out, which Foot to inject? Just because
LeftFoot extends/implements Foot? Analysing type inheritance is too
much magic. If there a dependency fo Object, you would also get a
LeftFoot injected.

Even though private modules is not that intuitive than your
suggestion, it is type safe and comprehensible.

Regards, Sven

Jeff Axelrod

unread,
Nov 21, 2012, 2:48:10 PM11/21/12
to google...@googlegroups.com
Take a look at Gimlet.  It uses Guice and provides better abstractions for the robot legs problem.  I've used it quite a bit.

Client code looks like this:

public class RobotLegModule extends AbstractModule {
 
@Override protected void configure() {
    bind
(Sneaker.class).to(NewBalanceSneaker.class);

    install
(new LegModuleBuilder()
       
.bind(RobotLeg.class)
       
.using(Sneaker.class)
       
.build()));
 
}
Reply all
Reply to author
Forward
0 new messages