清理/压缩由布尔变量表示的多个状态
在实现中,我有很多状态,由布尔变量表示,这样当一个状态为真时,针对该状态的变量为真,而许多或所有其他状态为假。这不是一个好方法,而且由于我只是在编码之前规划我的解决方案,所以结果是这样的。现在我必须清理它。最好的清洁解决方案是什么?
我正在考虑枚举,为每个状态命名,这样一个变量可以包含状态名称而不是多个变量。但问题是,还有与某些状态相关的其他信息,例如 Point 或 int 变量的一两个实例,它们仅适用于该状态。如果使用枚举如何容纳它们?
在这种情况下,最优雅、最合适的解决方案是什么?
In an implementation, i have a lot of states, represented by boolean variables, in such a way that when a state is true, variable against that state is true, and many or all of the others are false. It is not a good way, and since i was only planning my solution before coding, it turned out to be like this. Now i have to clean it. What is the best possible solution to clean?
I was thinking about enum, give a name to each state, and this way one variable can contain the state name instead of multiple variables. But the problem is that there is other information associated with some states, such as one or two instances of Point or int variables, which are only for that state. How to accommodate them if enum is used?
What is the most elegant and appropriate solution in situations like these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下状态设计模式来实现状态机,而不是将状态映射到枚举,每个状态都是一个类,事件是状态类的成员函数。
Have a look at the state design pattern to implement state machines, instead of mapping states to an enum, each state is a class, events are member functions of the state classes.