samedi 15 août 2015

A singleton-like manager class, better design?

I'm making a game engine and I'm using libraries for various tasks. For example, I use FreeType which needs to be initialized, get the manager and after I don't use it I have to de-initialize it. Of course, it can only be initialized once and can only be de-initialized if it has been initialized.

What I came up with (just an example, not "real" code [but could be valid C++ code]):

class FreeTypeManager
{
private:
    FreeTypeManager() {} // Can't be instantiated

    static bool initialized;
    static TF_Module * module;  // I know, I have to declare this in a separate .cpp file and I do

public:
    static void Initialize()
    {
        if (initialized) return;

        initialized = true;
        FT_Initialize();
        FT_CreateModule(module);
    }

    static void Deinitialize()
    {
        if (!initialized) return;

        initialized = false;
        FT_DestroyModule(module);
        FT_Deinit();
    }
};

And for every manager I create (FreeType, AudioManager, EngineCore, DisplayManager) it's pretty much the same: no instances, just static stuff. I can see this could be a bad design practice to rewrite this skeleton every time. Maybe there's a better solution.

Would it be good to use singletons instead? Or is there a pattern suiting for my problem?

Aucun commentaire:

Enregistrer un commentaire