开发者

Cleaning/Compacting multiple states represented by boolean variables

开发者 https://www.devze.com 2023-04-07 01:11 出处:网络
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. I

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消