Re: finite state machine with enum
On 2/10/2010 3:12 PM, andijcr wrote:
On 10 Feb, 19:15, Lew<no...@lewscanon.com> wrote:
andijcr wrote:
The main issue then is: how can I make a field private (not static)
RawProtocol accessible (for reading at least) to Mlsm, in an elegant
way and to objects?
Pass it as an argument to the enum 'exec' method.
--
Lew
I don't really like this solution - the field in the real code is
needed only in one of the eight states (while other states could in
the future require access to other fields).
My point here was to find if an elegant implementation is possible -
which seems impossible in the way i imagined.
Eventually my current solution works and is object-oriented. thanks
for your interest anyway.
Using similar to the flyweight pattern. I often have the following design:
public class State {
private int someStateField;
private String someOtherStateField;
StateNode node = StateNode.A;
public void transition(Object input) {
node.transition(this, input);
}
private enum StateNode {
A { public void transition(State state, Object input) { ... }},
B { public void transition(State state, Object input) { ... }},
C { public void transition(State state, Object input) { ... }};
public abstract void transition(State state, Object input);
}
}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>