Reputation: 7457
Lets say "we" have some boolean flags that dictate how an app behaves at any given time... anything from how touches are handled to what happens in onPause().
boolean touchMode_BlockAllExceptCreateNew = false;
boolean touchMode_UserIsEditting = false;
boolean touchMode_Animation4IsTakingPlace = false;
There could be a potentially infinite number of these little guys. :)
These "states" are mutually exclusive -- only one can or should be legally happening at a time.
So... isn't there some clever way of enforcing that mutual exclusivity by combining them into one variable?
Is using an Enum pretty much the only way to do this? Like this:
private enum TouchMode {
flibbidyInProgress,
deletePending,
userIsEditting,
jumpingJacks,
recyclerViewIsOpen,
normal // none
}
TouchMode currentTouchMode = TouchMode.normal;
private void changeTouchMode(TouchMode desiredTouchMode) {
boolean undesirableCondition1 = (desiredTouchMode == currentTouchMode);
boolean undesirableCondition2 = (desiredTouchMode != TouchMode.normal
&& currentTouchMode != TouchMode.normal);
if (undesirableCondition1 | undesirableCondition2) {
Log.e("XXX", ">> ILLEGAL TOUCHMODE REQUEST! <<");
Log.e("XXX", "requested: " + desiredTouchMode
+ ", current: " + currentTouchMode);
} else {
currentTouchMode = desiredTouchMode;
}
}
I thought I had seen some clever way of making the 'currentTouchMode' an int and assigning hex constants to it.
Upvotes: 0
Views: 343
Reputation: 1832
You should read about Strategy
OOP pattern, it's used for similar cases.
Here is some useful links:
If shortly - you should create class hierarchy with common interface and class-specific implementations for each TouchMode
(f.e. AnimationTouch
, EditionTouch
, etc.). Then you should use them via your common interface.
Upvotes: 1