Is it considered bad style to have that BOTH
value in the Direction enum
? Problems can arise from BOTH
being present, for example when trying to build a procedural UI for the Car class, it (the generator) would see the setTurnSignal(Direction)
method and create a way for the user to give it a Direction, but it would suggest all values, of which one is illegal.
Conversely, not having BOTH
causes issue with the getBlinkerStatus()
as it is no longer able to indicate that both are on. There are a couple ways I could address that issue, such as creating more blinker getters, but that or switching form an enum
to bit based values and building masks for them.
enum Direction{
LEFT, RIGHT, BOTH, NONE;
}
class Car{
Direction turnSignal;
boolean hazards;
void setTurnSignal(Direction direction){
if(Direction.BOTH.equals(direction)){
throw new IllegalArgumentException("Turn signal cannot be set to the given direction: " + direction);
}else{
turnSignal = direction;
}
}
Direction getBlinkerStatus(){
if(hazards){
return Direction.BOTH;
}else{
return turnSignal;
}
}