清理/压缩由布尔变量表示的多个状态

发布于 2024-12-06 11:36:03 字数 269 浏览 1 评论 0原文

在实现中,我有很多状态,由布尔变量表示,这样当一个状态为真时,针对该状态的变量为真,而许多或所有其他状态为假。这不是一个好方法,而且由于我只是在编码之前规划我的解决方案,所以结果是这样的。现在我必须清理它。最好的清洁解决方案是什么?

我正在考虑枚举,为每个状态命名,这样一个变量可以包含状态名称而不是多个变量。但问题是,还有与某些状态相关的其他信息,例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-12-13 11:36:03

看一下状态设计模式来实现状态机,而不是将状态映射到枚举,每个状态都是一个类,事件是状态类的成员函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文