I have
public enum BaseActions implements Actions{
STAND (0,0,0),
TURN (1,1,1);
//other stuff
}
public enum CoolActions implements Actions{
STAND (0,2,3),
TURN(1,6,9);
//other stuff
}
public enum LooserActions implements Actions{
STAND (0,-2,-3),
TURN(1,-6,-9);
//other stuff
}
public interface Actions {
//interface methods
}
class A {
Actions mCurrentAction;
protected void notifyNewAction(final Actions pAction, final Directions pDirection){
//body of the method
}
public void doStuff(final Actions pAction) {
if(pAction.getMyId() > 0)
notifyNewAction(BaseActions.STAND, myDirection);
else
notifyNewAction(BaseActions.TURN, myDirection);
}
}
class B extends A{
public void doMyStuff() {
doStuff(CoolActions.STAND);
}
}
class C extends A{
public void doMyStuff() {
doStuff(LooserActions.STAND);
}
}
i would like to make A use CoolActions when doStuff is called from B and LooserActions when called from C.
One of the ways i think i can do it is to use generics, and then in B and C use
doStuff<CoolActions>(CoolActions.STAND)
and have in A
public void doStuff<T extends EnumActions&Actions>(final Actions pAction) {
if(pAction.getMyId() > 0)
notifyNewAction(T.STAND, myDirection);
else
notifyNewAction(T.TURN, myDirection);
}
where EnumActions is a base enum that just contains the declaration of the enum’s elements, and nothing more, something like an interface for enums, but enums can’t extend another class since they already extends Enum, and an interface can’t provide what i mean.
Another way would be to make the enums implements a EnumActions interface that has
public interface EnumActions {
public <T> T getStand();
public <T> T getTurn();
}
and have
class A {
Actions mCurrentAction;
protected void notifyNewAction(final Actions pAction, final Directions pDirection){
//body of the method
}
public <T implements EnumActions> void doStuff(final Actions pAction) {
if(pAction.getMyId() > 0)
notifyNewAction(T.getStand(), myDirection);
else
notifyNewAction(T.getTrun(), myDirection);
}
}
and
public enum CoolActions implements Actions, EnumActions{
STAND (0,2,3),
TURN(1,6,9);
public CoolActions getStand();
public CoolActions getTurn();
//other stuff
}
class B extends A{
public void doMyStuff() {
doStuff<CoolActions>(CoolActions.STAND);
}
}
But 1)i don’t know if it would work 2) I lose the advanteges of using enums 3) this seams a really bad way to handle this 4) i would have to write a lot( X enum fields per Y different enums). I changed from static final fields to enum to improve readability and order, and this seams to make things even harder.
Am i designing this in the wrong way? How can i handle this?
Is there a preferred way to solve this problem?
Thanks
It seems like enums add nothing and are not going to do what you want. Maybe you should just use a normal class hierarchy – make
BaseActions,CoolActionsandLooserActionsjust classes that implementActionsand STAND and TURN methods in those classes.