How can I refactor my code to use fewer singletons?
Posted
by
fish
on Game Development
See other posts from Game Development
or by fish
Published on 2012-11-04T14:17:25Z
Indexed on
2012/11/04
17:17 UTC
Read the original article
Hit count: 165
I started a component based, networked game (so far only working on the server). I know why singletons can be bad, but I can't think of another way to implement the same thing. So far I have:
A GameState singleton (for managing the global state of the game, i.e. pre-game, running, exiting).
A World singleton, which is the root entity for my entity graph
An EntityFactory
A ComponentFactory
I'm thinking about adding a "MessageDispatcher" so individual components can subscribe to network messages.
The factories do not have state, so I suppose they aren't so bad. However, the others do have global state, which is asking for trouble. How can I refactor my code so it uses fewer singletons?
© Game Development or respective owner