Should the state machine expose its current state?

We use the "Machine Machine" structure (based on a state template) that does not reveal its current state, which sometimes means that I have to do things in a workaround.

When I questioned this design decision, one of the rationales was that "if you need to know the current state, you are using it incorrectly."

It is right? I don’t know much about state cars.

(I ask here because I know that I have an inherent bias against the state template, which I find too detailed.)

Example

Imagine a system that reads two sensors in one of these states. One sensor gives a numerical value, the other a logical one that tells you whether the first is “reliable” or not. The system displays a value that is either the current “good” value or interpolation (or some other fancy calculation) based on the last n good values.

My idea would be to have two substations - one “good” and the other “no”. And when a new value comes, I would like to ask the state machine in which it is indicated so that I know how to handle interpolation.

(I think I answered my own question: should the solution consist in the NewDataValue(val) event on the destination machine, which only sends the value from the "good" state?)

+8
design-patterns state-machines
source share
4 answers

I have to agree with the person who made the comment "use it incorrectly."

The entire finite machine is a black box in which events accumulate, and which cause certain things based on these events (including state transitions). Events themselves should not generally depend on the current state of the machine.

I cannot foresee a situation in which an event should change depending on the current state (but feel free to educate me if you have any).

An event will always be what it is. If it needs to be processed differently depending on the current state, this is a problem for the state machine itself, and not for event pumps.

In fact, the whole idea of ​​changing an event based on the current state flies before encapsulation.

The best state cars have a very simple form:

  +------+ | | state V | transitions +---------------+ | events --> | | --+ | state machine | effects <-- | | +---------------+ 

In other words, events are somehow pumped up (regardless of state) and have certain effects based on its state and events. And he maintains his own condition.


As for updating your question, in which you would like to handle the output differently depending on whether the last reading was good or a formula based on the previous ones, I would just put this in the effects section. Have a state machine to display the current value plus an indication of whether it was from a sensor or calculated.

Then your code that processes effects can do what it likes with this information. This effectively gives you the necessary information and does not violate the growth of the black box.

+10
source share

Well, that’s the real question. Why do you need to know the current state of the state machine? What do you plan to do with it?

If you second guess about this, it is trying to predict where the state machine will be in the future based on the current state and new inputs, then you start something in parallel with the state machine. I don’t know whether this is good or bad, depending on what and why you are doing it.

Ostensibly, a state machine is a black box (or, in some cases, it looks like a game machine!) Into which you feed data and extract data from.

Like programming, there is no need for things to be opaque black boxes. Things can always be clear cased boxes that have input slots and output slots, but you can at least see gears working in them. But this contradicts abstraction if you are trying to get around this, since the state machine is designed to encapsulate a logic block.

Thus, the opacity of the structure is not really a problem, it is what you are trying to do with the information, if or when you receive it.

+2
source share

"if you need to know the current state, you are using it incorrectly." - It is right.

The current state cannot be set. All things that require you to act differently, due to the fact that they are in the specified state, must be placed inside ( and only there ) this state - as private methods called from public, or as publicly available nothing (or what something else) in other states. Your "state machine" should work ... and you, looking from the outside, should not know why.

+2
source share

In the design of hierarchical systems, usually a higher-level object knows only the abstraction of the state of a lower-level object, for example. state_good abstractions (perfect, acceptable, ...) and state_bad abstractions (failed1, failed2, failed3 ..).

In non-hierarchical systems, it is normal for one object to know exactly the state of another object, especially if objects are assigned different tasks.

-Janusz

0
source share

All Articles