Robot leg problems... I don't understand it :(

297 views
Skip to first unread message

Anthony MULLER

unread,
Jan 22, 2009, 7:45:46 AM1/22/09
to google-guice
Can you explain me what is the case known as "robot legs problem" ?

I read FAQ and example here : http://docs.google.com/View?docid=dhfm3hw2_51d2tmv6pc

Basically, I don't see why not to write simply this :

public class LegModule extends AbstractModule {

   void configure () {
       bind(Foot.class).annotatedWith(Left.class).toInstance(new Foot("leftie"));
       bind(Foot.class).annotatedWith(Right.class).toInstance(new Foot("righty"));
   }
}

Note : gmail coding...

Thanks !
Anthony

Alen Vrečko

unread,
Jan 22, 2009, 2:03:49 PM1/22/09
to google-guice
Hi,

correct me if I am wrong guys,

The point is here

static class Leg {
@Inject Foot foot;

Notice you're injecting a Foot but how do you know which Foot?

You can't to

static class Leg {
@Inject @Left @Right Foot foot;

that makes no sense

static class Robot {
@Inject @Left Leg leftLeg;
@Inject @Right Leg rightLeg;

Now you can't do

bind(Foot.class).to(new Foot("Lefty")).ifParentAnnotatedWith
(Left.class);
bind(Foot.class).to(new Foot("Righty")).ifParentAnnotatedWith
(Right.class);

but notice how with Private Module you can get this functionality.

I've been playing around with this and made an example using a Car
maybe that will be clearer. I get confused with Feet and Leg on first
reading.

Cheers,
Alen

/////////////////////////////////////////////////////////////////////////////////
import com.google.inject.*;
import com.google.inject.privatemodules.PrivateModule;

import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Date;

public class RobotLegsProblem2 {


public static void main(String[] args) {
Injector injector = Guice.createInjector(new AbstractModule()
{
@Override
protected void configure() {
// global stuff
// same to all
bind(Driveline.class).to(FrontWheelDrive.class);
bind(Engine.class).to(DieselEngine.class);
}
}, new PrivateModule() {
@Override
protected void configurePrivateBindings() {
// private Module is different story
// Bind car annotated with blue and expose it
bind(Car.class).annotatedWith(Blue.class).to
(Car.class);
expose(Car.class).annotatedWith(Blue.class);

// What we bind in here only applies to the exposed
stuff
// i.e. the exposed car from this module will get this
injected
// where stuff in regular module (Engine,Driveline) is
"inherited" - it is global
bind(Transmission.class).to
(AutomaticTransmission.class);
}
}, new PrivateModule() {
@Override
protected void configurePrivateBindings() {
bind(Car.class).annotatedWith(Red.class).to
(Car.class);
expose(Car.class).annotatedWith(Red.class);

bind(Transmission.class).to(ManualTransmission.class);
// The point is that you cannot do this with regular
module i.e.
// bind(Car.class).annotatedWith(Blue.class).to
(Car.class);
// bind(Car.class).annotatedWith(Red.class).to
(Car.class);
// now notice the dilemma
// class Car{
// @Inject Transmission transmission;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// You cannot solve this by
// @Inject @Blue @Red Transmission transmission;
// or
// bind(Transmission.class).to
(AutomaticTransmission.class).ifParentAnnotatedWith(Blue.class)
// bind(Transmission.class).to
(ManualTransmission.class).ifParentAnnotatedWith(Red.class)
// but with private modules you can get this
functionality
}
});

Car blueCar = injector.getInstance(Key.get(Car.class,
Blue.class));
System.out.println("Blue car transmission: " +
blueCar.getTransmission());

Car redCar = injector.getInstance(Key.get(Car.class,
Red.class));
System.out.println("Red car transmission: " +
redCar.getTransmission());

}


}

@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, PARAMETER, METHOD})
@BindingAnnotation
@interface Blue {
}

@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, PARAMETER, METHOD})
@BindingAnnotation
@interface Red {
}

class Car {

private final Engine engine;
private final Transmission transmission;
private final Driveline driveline;

@Inject
public Car(Engine engine, Transmission transmission, Driveline
driveline) {
this.engine = engine;
this.transmission = transmission;
this.driveline = driveline;;
}

public Driveline getDriveline() {
return driveline;
}

public Engine getEngine() {
return engine;
}

public Transmission getTransmission() {
return transmission;
}
}


interface Transmission {
}

class AutomaticTransmission implements Transmission {
}

class ManualTransmission implements Transmission {
}

interface Engine {
}

class DieselEngine implements Engine {
}

class PetrolEngine implements Engine {
}

interface Driveline {
}

class FourWheelDrive implements Driveline {
}

class FrontWheelDrive implements Driveline {
}

class RearWheelDrive implements Driveline {

Brian Pontarelli

unread,
Jan 22, 2009, 6:03:13 PM1/22/09
to google...@googlegroups.com
This code is pretty rough. Try sending your emails as RTF or setting
your right margin to something like 150 or 200.

-bp

Alen Vrečko

unread,
Jan 22, 2009, 7:37:14 PM1/22/09
to google-guice
I copied it from text editor. Been wondering how one pretty prints the
code. There is no <code>print "Hello, World"</code> possibility or
something like that.

Thanks for the suggestions.

Cheers,
Alen

Rick

unread,
Jan 22, 2009, 7:56:09 PM1/22/09
to google...@googlegroups.com
Can you paste it here http://pastie.org/ or here http://pastehere.com/Paste.aspx
and link  it. (choose java for format)
--
Rick

Alen Vrečko

unread,
Jan 22, 2009, 8:26:44 PM1/22/09
to google-guice
Great! Didn't know about pastie or pastehere. Thanks.

The code once more http://pastie.org/368348

Cheers,
Alen

On Jan 23, 1:56 am, Rick <ric...@gmail.com> wrote:
> Can you paste it herehttp://pastie.org/or herehttp://pastehere.com/Paste.aspx

Eric Anderson

unread,
Jan 22, 2009, 9:22:07 PM1/22/09
to google...@googlegroups.com
Better,

http://pastebin.com/

Supports Groovy syntax highlighting.

Eric

2009/1/22 Alen Vrečko <alen_...@yahoo.com>

limpb...@gmail.com

unread,
Jan 22, 2009, 11:37:22 PM1/22/09
to google-guice
On Jan 22, 5:26 pm, Alen Vrečko <alen_vre...@yahoo.com> wrote:
> The code once morehttp://pastie.org/368348

Great example. I've added it to the FAQ.
http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions

Off topic -- if the FAQ is missing anything, you can leave a comment on
that page and we'll address it.

Gili Tzabari

unread,
Jan 23, 2009, 12:00:06 AM1/23/09
to google...@googlegroups.com

I don't think you should link to pastie. They will eventually remove
the link. How about copying it inline into the FAQ?

Gili

Anthony MULLER

unread,
Jan 23, 2009, 4:06:26 AM1/23/09
to google...@googlegroups.com
Thanks for the complete explanation!

Anthony


2009/1/23 Gili Tzabari <gili.t...@gmail.com>

Josh Goebel

unread,
Jan 23, 2009, 1:16:13 PM1/23/09
to google-guice
For all intents and purposes public pasties linked from other sites
will never expire. Private pastes do expire and public pastes that
someone pastes and no one ever looks at can be removed as well. But a
public pastie with incoming referrers isn't going anywhere.

Pastie author,
Josh (Dreamer3)

Brian Pontarelli

unread,
Jan 23, 2009, 6:18:04 PM1/23/09
to google...@googlegroups.com
I'm constantly and pleasantly surprised by how many people are on
various lists I'm on. It's really cool to see all these folks popping
up and filling in the blanks.

-bp
Reply all
Reply to author
Forward
0 new messages