style:access singleton

Started by
16 comments, last by Zoomby 20 years, 10 months ago
I really don''t see that being realistic in many cases. You made some really good points, and I''ll be thinking about them.

The thing to keep in mind is that a Singleton pattern is a valid tool. Whether or not you find it useful doesn''t negate it''s validity. If you prefer not to use it, then don''t.
daerid@gmail.com
Advertisement
Yes, sometimes I end up being pretty fanatic against singletons even though I have to admit that often the solution that gets the job done quickest is the most beneficial. And that may mean using singletons. Yet, IMHO, code without singletons or other globals would be best code (easiest to extend and understand), but if it takes a lot longer to make it work so, it just may not be worth it.
quote:daerid: I don''t want to have to store a reference to the Engine object in every freakin other class I make that needs to access it.

The problem is once you''ve made the engine a singleton you''ll just let every and any class access it (because they can). You would have a better design if you didn''t use a singleton and actually had to tackle thinking about dependencies, reusability etc.

If you think the only alternative to a singleton is storing a reference to the class in every class that needs to know about it then you have a lot to learn.
If you think the only alternative to a singleton is storing a reference to the class in every class that needs to know about it then you have a lot to learn.

What would be the other alternatives?

Not being sarcastic here.. i''ll happily search for details of the info, if only someone could provide me with relevant terms for the web search engines, so i know _what_ i should look for...
I''m actually using templated singletons atm because it gives me a much greater module support and easy to access style and it makes it extremely easy to port a module to another project.

This is my singleton class that is extremely stable.

  template<typename Class>class CSingleton{protected:	static Class* m_pInstance;public:	static Class* GetInstance()	{		if (!m_pInstance)			m_pInstance = new Class;		return m_pInstance;	}	static void Delete()	{		if (m_pInstance)		{			delete m_pInstance;			m_pInstance = NULL;		}	}};template<typename Class>Class* CSingleton<Class>::m_pInstance = NULL;  


So whenever I need to create a templated singleton manager class its just


  class CSomeManager : public CSingleton<CSomeManager>{};inline CSomeManager* SomeManager(){	return CSomeManager::GetInstance();}  


I always have Initialise and Uninitialize functions aswel in my manager classes, the only thing the coder needs to do is make sure the delete function is called for every Manager class to ensure that the memory is freed. (I put it at the end of its own Uninitialize function)
Of course I do have some rules when using the such as that the class mush be self-sustained meaning that if it needs a variable to work, say HINSTANCE, then I wont be asking for it from another singleton but I'd have it sent as a parameter in the initialize function.

[edited by - Cybrosys on May 27, 2003 9:33:48 PM]
singletons do have uses, but normally get quite quickly abused by newbies (newbies for singletons!) as global variables. you''re bether off defining a global variable then..:D

with nice code design, dependenties are shrinking, and the need to "access some object from everywhere" gets small. the use of the singleton is not really a use anymore.

you should not have tons of calls to the engine. the engine should run by itself, and automagically handle your world. your objects. your events. without you talking to it.

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Exactly.

This topic is closed to new replies.

Advertisement