I'm trying to implement a feature with option to turn it on/off based on a flag. I'm doing it in the following way which I feel like a overkill,
public interface Feature {
public interface EnableState {
void execute();
}
public interface DisableState {
void execute();
}
}
Exposed an interface so that each and every feature in my project could make use of it to define their own way of enabling or disabling a feature.
public class PlaySound implements Feature.DisableState, Feature.EnableState {
//Feature is turned off by default
boolean status = false;
private static final PlaySound playSound = new PlaySound();
public static PlaySound getInstance() {
return playSound;
}
private PlaySound() {
//Compute and update the feature status
}
Feature.DisableState disabledState = new Feature.DisableState() {
@Override
public void execute() {
}
};
Feature.EnableState enabledState = new Feature.EnableState() {
@Override
public void execute() {
}
};
@Override
public void execute() {
if(status) enabledState.execute();
else disabledState.execute();
}
}
Which I feel like a overkill. Does the code smell bad?
Instead should I have implemented in this way,
if(featureEnabled) {
//do something
} else {
//do nothing
}