How to use state pattern correctly?

后端 未结 9 702
既然无缘
既然无缘 2021-01-29 21:54

I\'ve encountered a few implementations of state pattern in my programming experience, and done a few. I\'ve seen them used in various scenarios (mostly UI and parsing). The tro

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 21:59

    I am building a expression evaluator which has the ability to evaluate sets of elements. I found the state pattern very useful for discriminating what can and can't be done to a set depending on its state. ie: open,closed, inactive, active ect. FSM are very easy to draw and reduce the complexity of code by removing the need for huge blocks of ifelse statements to define what the feature should do depending on its enclosed attributes. It makes these conditions more explicit by making the conditions into classes. It's one of my favourite patterns so far.

提交回复
热议问题