如果没有不同的行为,您可能不需要为每个状态创建一个类。您可能只有一个 State 类和一个 FSM 来管理状态集合和管理转换的规则。
My question how should the state machine be designed ,should there be individual SM for every class ?
If I understand your description, the answer is "yes", each object would require its own state machine.
Can all states have a common base interface ?
A common interface implies common API with multiple implementations. What common methods will each state have, and how do the implementations differ by state?
You may not need a class for each state if there's no different behavior. You may just have a State class and a FSM that manages a collection of States and the rules that govern transitions.
In my humble experience, the state design pattern is somehow hard to use, as it tends to generate a lot of boilerplate code. I usually prefer the standard function pointer approach of the problem. It is an easier way if your objects / states are not too big / too complex.
发布评论
评论(2)
如果我理解你的描述,答案是“是”,每个对象都需要自己的状态机。
通用接口意味着具有多种实现的通用 API。每个州都有哪些通用方法,各个州的实施有何不同?
如果没有不同的行为,您可能不需要为每个状态创建一个类。您可能只有一个 State 类和一个 FSM 来管理状态集合和管理转换的规则。
If I understand your description, the answer is "yes", each object would require its own state machine.
A common interface implies common API with multiple implementations. What common methods will each state have, and how do the implementations differ by state?
You may not need a class for each state if there's no different behavior. You may just have a State class and a FSM that manages a collection of States and the rules that govern transitions.
根据我的拙见,状态设计模式在某种程度上很难使用,因为它往往会生成大量样板代码。我通常更喜欢解决问题的标准函数指针方法。如果您的对象/状态不太大/太复杂,这是一种更简单的方法。
我的2c
In my humble experience, the state design pattern is somehow hard to use, as it tends to generate a lot of boilerplate code. I usually prefer the standard function pointer approach of the problem. It is an easier way if your objects / states are not too big / too complex.
my2c