How to avoid single games when creating the design of state-owned OOP machines?

I am in the center, trying to teach myself programming. I started the same way as most people. making small, dirty apps and games that make simple things not so easy. I recently tried to take the next step by writing a slightly more complex game that uses OOP design to write better, more modular code!

The main problem I ran into is the design of my main StateManager (FSM) class (for switching between I / O / menu / game / etc / states). I looked high and low, and I really saw two methods for designing them:

  • Use the switch / case + enum statement to switch between states.

  • Make a singleton FSM class that handles pushing / popping states to / from a vector.

Now, my problem is that the case switch statement is very repetitive and clumsy, and this works against my goal of using this project to teach myself OOP.

My second and biggest problem is the "singleton" sentence.

As I said, I try to teach myself, and I still have a lot to learn when it comes to programming, especially in the area of ​​OOP and design patterns and all that. I ran into a problem when for EVERY single “singleton” is an evil thread and discussion that I find, I find as many tutorials and links where people use single code in their code to create “motor” classes and FSM. This is a very consistent mixed message.

I suppose I just don’t understand why ... Even if you want / want to have only one class object, why is it necessary / useful to make the constructor private and create a singleton? I read a lot about how singles are bad, how they are essentially global, how they interfere with multithreading, and how many programmers consider them excessive or just bad projects ... But I see an example after the example of people using them, and very few examples examples showing alternative methods.

Is it possible to do the same with a regular class? What is the purpose of explicitly restricting instantiation? I only heard about negative things about singles, but people seem to use them all the time ... Am I missing something about singles and OOP?

Is singletons just a trend, or is it just a trend when people call loneliness? How do I get around this? Is there something between the FSM switch / foot and the singleton FSM ?? Could someone create their own state system of programs in the same way, without making any of their classes in single games? Will this change anything? [confused]

+8
c ++ design oop singleton fsm
source share
3 answers

Is it possible to do the same with a regular class? What is the purpose of explicitly restricting instantiation? I only heard about negative things about singles, but people seem to use them all the time ... Am I missing something about singles and OOP?

No, I believe that you are on the right track: there is absolutely no reason to prevent the creation of two copies. Because of this, the universe will not collapse.

This is a completely unnecessary limitation. Since you must manually encode this restriction, it is completely foolish for you to have it at all (if you had to write code to raise it, that would be another matter).

I suppose there may be strange and rare situations where the Universe unleashes if a second instance is created, but that sounds pretty far-fetched. In any case, this does not justify the predominance of singletones all over the Internet.

I can’t explain why people use them all the time, without offending the rational abilities of these people, so I will refrain from this.

Could someone correctly develop their state system of programs in exactly the same way, without making any of their classes solitary? Will this change anything?

The only thing that he will change is that, having made him single, you do not allow yourself to create extra instances for unit testing. Everything else works the same way.

+6
source share

As often happens, there is no simple answer. In C ++, a singleton pattern also controls the order of initialization issues, which is important if it should be used in static object constructors. And there are certain types of objects in which only a few instances can be used to be programming errors; if you can prevent them, why not.

On the other hand, it’s hard for me to imagine the case where a StateManager class should be single. This is in no way the management of some unique resource or some meta-functionality (for example, logging), which by its nature must be unique. And I can’t imagine it is also used in static object constructors.

A statement such as “singles is bad” is very misleading; for some things, they are the most appropriate solution. But just because they are not bad in the absolute does not mean that they are relevant everywhere. They solve one specific, very limited problem. When you need to solve this problem and not use singleton badly. But (as there is true for almost everything), using it when it does not fit is bad.

+2
source share

I do not understand why the FSM class should be single. There are many real FSM systems where there are no singletones, hell, there are some where state instances are created and destroyed when SM crosses states, and where the state machine itself is a state that makes it easy to compose hierarchical state machines.

In a complex application, you will have several states, some of which appear and disappear. Those that handle, for example, client-server interaction, will necessarily have several instances that come and go with clients. Some others may benefit from compiling several hierarchical state machines, some of which can be reused in many places. So the idea that you will have a state machine as a singleton class is silly for me. For example, you might have an instance of a specific state machine in a single-user application class that encapsulates your application, but it is far from having one state machine.

To get a good overview of the various ways to implement state machines, including the contrasts between flat FSMs and hierarchical state machines (HSMs), you would like to look at Miro Samek PSICC2 . He has many freely downloadable articles explaining common methods.

A reasonable C ++ implementation for a hierarchical state machine, suitable for use in the user interface, is available in the machine state system in Qt .

+1
source share

All Articles