I've come across some examples of state machines written as enumerations. For instance:
public enum State {
INITIAL {
@Override
public State process(char ch) { return MIDDLE; }
},
MIDDLE {
@Override
public State process(char ch) {
switch (ch) {
case 'a': return INITIAL;
default: return FINAL;
}
}
},
FINAL {
@Override
public State process(char ch) { return FINAL; }
};
public abstract State process(char ch);
public static State initial() { return INITIAL; }
}
What concerns me, is having the constants know each other. Their implementations are subject to change, and if constants are added (or removed) more changes are implied.
However, all changes are contained in the enumeration, unless outside code references the constants directly and makes assumptions about the transition table.
So, is this code highly coupled, loosely coupled, or is it subjective?
I'd say this is too tightly coupled for a flexible tool.
Something looser would be much more flexible: