Implementing undo in a state-machine based application

58 Views Asked by At

I have an application whose core design is based on a state machine that uses one-way transitions. This design works well for most scenarios and required features; however the application requires an additional "undo" feature that is available during some of the states. Since many states can have transitions to a single state, this undo feature forces me to keep track of the previous state. To go back to the previous state, I can either:

  1. force the previous state that I kept, regardless of existing transitions or:
  2. create many new "undo" transitions to previous states, and choose the correct undo transition according to the previous state

As I currently see it, both options somewhat break the state machine concept. The second option is less violent but perhaps more clumsy in an "if-else" matter and more vulnerable to bugs.

Which option do you think is preferable? I would love to hear other ideas if you have any.

I am currently using the second suggested method, but it's quite ugly and requires some maintenance

0

There are 0 best solutions below