Singletons alternative
c++, design-patterns, oop
Solution
You could have a global "game" object that creates an instance of each of the classes that are currently singletons
For the specific example of your EventManager; your Listener base class could provide implementations of a register method and a push method that derived classes can call.
A skeleton definition:
class Listener
{
public:
virtual void ReceiveMessage( ... ) = 0;
protected:
void Register()
{
GetEventManagerSomehow()->RegisterListener( this, etc );
}
void PushEvent( etc )
{
GetEventManagerSomehow()->PushEvent( etc );
}
}
Problem
I have a pretty simple game engine. It uses several singletons(I'll enumerate some of them). ``` Resource Manager Render Engine Events Manager Factory etc ``` These singletons have many calls from one to another. I'll take Events Manager sample usage: - Any object derived from Listener can add itsel as a listener for some events just like this `EventsManager->RegisterListener(this, &SomeClass::SomeMethod);` (event type is deduced by SomeMethod parameter) - Any other object can fire an event like this `EventsManager->PushEvent(SomeEvent);` After some synchronization the event reaches to all listeners. This is very a simple usage for EventsManager when it is singleton. Similar behavior is with other singletons. I want to remove the singletons, but my main problem is that I want to keep the code simple to use from the "user point of view" as it is now. I read some techniques of doing this, but most of the make the initialization/usage of the classes more complicated. I know this topic was discused many times on SO, but no answer is appropriate for my programming philosophy - to keep everything as simple as possible. I don't want to have complicated definition/initialization for my classes like: ``` SomeClass<EventManager, RenderEngine,...> ``` or ``` SomeClass::SomeClass(EventsManager, RenderEngine...) ``` Can you please give me some advice on this topic?