Suppose I have the following base class, Queen and Knight as its derivatives. WeaponBehaviour is an interface. I can't figure out how to inject weapons using Guice depending on the concrete GameCharacter type.
public abstract class GameCharacter {
@Inject
protected WeaponBehaviour weapon;
public GameCharacter() {
}
public void fight() {
weapon.useWeapon();
}
public void setWeapon(WeaponBehaviour weapon) {
this.weapon = weapon;
}
}
You could use Binding Annotations.
A subclass:
class GimliSonOfGloin extends GameCharacter {
@Inject
public void setWeapon(@Axe WeaponBehaviour weapon) {
super.setWeapon(weapon);
}
}
The Annotation:
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
public @interface Axe {}
The Binding:
bind(WeaponBehaviour.class)
.annotatedWith(Axe.class)
.to(MyAxe.class);