Jump to content
  • Advertisement
Sign in to follow this  
Nickie

A design pattern question

This topic is 2214 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I've got main engine app object, and there shoud be only one instance of it. There are many subsystems (resource cache, asset manager, scene graph, etc). Which is the best way to access and create them.(All of them can have only one instance)

Currently I'm using 2 static global object for each system + a static get method.
static AssetManager * g_pAssetManager = NULL;
static AssetDestructor g_AssetManagerDestructor; // omfg I could have used boost's shared ptr to remove this class...
static AssetManager * const AssetManager::Get();

On Get() I'm checking if g_pAssetManager = NULL and allocate it. Then I return the ptr;
g_AssetManagerDestructor just check if g_pAssetManager is different than NULL and delete it;

However some systems are using another systems in their destruction process and this can cause runtime failure(destory A.destroy B using A's methods)

Any ideas how I can keep these sytems so I can control their destruction order?
(I have tried something with the resource cache already. To save some headaches and saved the get method, but I moved the global pointer into the engine app object. So it is actually doing:
[source lang="cpp"]static ResourceCashe * const ResourceCache::Get()
{
return RandomEngineApp::Get()->GetResCache();
}[/source]
Any suggestions? I've got poor design pattern knowledge(only factories, I've heard of singleton pattern but I'm not really sure if I'm using it tongue.png) Edited by Nickie

Share this post


Link to post
Share on other sites
Advertisement
Yea you're using singleton pattern, and seeing some problems what they can cause. If you create the systems normally and give global access to them, you can control the construction and destruction orders.


// AssetManager.h file
class AssetManager {};
// Global accessor.
AssetManager& DefaultAssetManager();
void SetDefaultAssetManager(AssetManager* manager);

// AssetManager.cpp
namespace {
AssetManager* g_DefaultAssetManager = 0;
}
AssetManager& DefaultAssetManager()
{
assert(g_DefaultAssetManager);
return *g_DefaultAssetManager;
}
void SetDefaultAssetManager(AssetManager* manager)
{
g_DefaultAssetManager = manager;
}

// main.cpp
#include "AssetManager.h"
int main()
{
{
// Here you manage the lifetime of these objects, aswell construction
// and destruction order.
AssetManager manager;
SetDefaultAssetManager(manager);
OtherSystem system(manager);
}
SetDefaultAssetManager(nullptr);
}
Edited by Codarki

Share this post


Link to post
Share on other sites
Yeah really nice. I;ve seen this in many other project. I think it will be perfect for me.
Thanks.

Share this post


Link to post
Share on other sites
Singleton is an anti-pattern, and should be avoided. It's really just a global variable, which is also evil, and should be avoided.
Any ideas how I can keep these sytems so I can control their destruction order?
Destruction order is normally the opposite of construction order, which works fine:class App
{
App() : a(), b(a) {};
//~App() calls b->~(), then a->~()
UsedByOthers a;
DependsOnA b;
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!