Design Pattern problem involving N states and transitions between them

后端 未结 6 1393
春和景丽
春和景丽 2020-12-12 16:44

I have a problem at hand and I am not getting which design pattern to use. The problem goes as such:

I have to build a system which has \'N\' states and my system ha

6条回答
  •  一向
    一向 (楼主)
    2020-12-12 17:18

    As others have said a state machine can be modelled in procedureal code with a switch or in OO code with the state pattern, which is probably what you were after.

    However a 3rd way is to actually code it as a graph, with states as nodes and conditions as directed edges. The visitor pattern can then be used to apply the graph to different uses. this would lend itself particularly well to designs where the states and/or the transitions could be user defined, but would probably be more memory intensive then the hard coded state machines described in the other answers.

提交回复
热议问题