• Advertisement
Sign in to follow this  

How to avoid singleton

This topic is 1897 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

I started a new project and I feel like I'm using singleton for everything.

For instance, I have skills that a character can do. I implemented that as singleton
[source lang="cpp"]class IceSpear;
class FireBall;
class Sprint;
class Teleport;[/source]
And to use one of the skill
[source lang="cpp"]character.doSkill(Fireball::getInstance());[/source]

I know I shouldn't use singletons a lot. How can I avoid this?

Share this post


Link to post
Share on other sites
Advertisement
Is there a reason Fireball needs to be a singleton and you can't just do
character.doSkill(new Fireball());

Share this post


Link to post
Share on other sites
The reason I decided to use singletons is that there's going to be only one instance of Fireball at all times and rather than calling new and delete, I thought it would be better to just make it singleton

wait.. I just got an idea that forced me not to use singleton.
thanks, anyways. Edited by lride

Share this post


Link to post
Share on other sites
yeah... you could just use a const to record the maximum number of fireballs, and a static member variable to record the current number of fireballs, and set the max at 1 and use a factory to instantiate a fireball which checks to see if the maximum number of fireballs has been reached before allocating memory for another instance... (totally off the top of my head, so don't trust me)

The main reason singletons are bad is because they're pretty much like publicly accessible global variables. If joe down the hallway decides that he wants to use your singleton and he mucks around with some internal variable values in his code and then you muck around in some other code which mucks around with internal variables, you're going to run into some dependency issues which will inevitably manifest themselves as bugs which are really hard to track down. Or, you'll develop your code around the assumption that the singleton is always going to be the lone instance, but five weeks later, your boss/customer/good idea fairy comes down and says "yeah... we want more than one fireball." and that's going to cause some serious refactoring of code. If you use singletons, expect to be up to your knees in muck, stumbling around and mumbling "fuck."

Just.... entirely forget what a singleton is. Erase it from memory.

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1352049360' post='4997226']
The reason I decided to use singletons is that there's going to be only one instance of Fireball at all times and rather than calling new and delete, I thought it would be better to just make it singleton

wait.. I just got an idea that forced me not to use singleton.
thanks, anyways.
[/quote]
When you come up with a solution to a problem you've asked about, please post that solution. Someone in the future may search for something related, find your post, and just see "nevermind, I figured it out", which is frustrating. You're not helping anyone by not posting it. Posting your idea may also spur more discussion that leads to even better ideas.

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1352048408' post='4997219']I know I shouldn't use singletons a lot. How can I avoid this?[/quote]
You can use a factory, then pass the factory around:

[code]
class Factory {
private:
map<string,Entity*> prototypes;

public:

Entity* create(const string&amp; name) {
if ( prototypes[name] )
return prototypes[name];
}

// Or:
// template<class T>
// T* create() { }
// template<> Fireball* create() { return (Fireball*)prototypes["fireball"] /*or etc...*/; }
};

struct Game {
Factory f;
};

void test( Character&amp; c, Factory&amp; f ) {
c.doSkill(f.create("fireball"));
// or: f.create<Fireball>()
}
[/code]
Or etc...

You can also use [url=http://stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap]const char*[/url] over std::string if you need to avoid frequent memory allocations there.. Edited by fastcall22

Share this post


Link to post
Share on other sites
<p>[quote name=&#39;Suspense&#39; timestamp=&#39;1352138362&#39; post=&#39;4997648&#39;]<br />
[quote name=&#39;lride&#39; timestamp=&#39;1352049360&#39; post=&#39;4997226&#39;]<br />
The reason I decided to use singletons is that there&#39;s going to be only one instance of Fireball at all times and rather than calling new and delete, I thought it would be better to just make it singleton<br />
<br />
wait.. I just got an idea that forced me not to use singleton.<br />
thanks, anyways.<br />
[/quote]<br />
When you come up with a solution to a problem you&#39;ve asked about, please post that solution.  Someone in the future may search for something related, find your post, and just see &quot;nevermind, I figured it out&quot;, which is frustrating.  You&#39;re not helping anyone by not posting it.  Posting your idea may also spur more discussion that leads to even better ideas.<br />
[/quote]<br />
<br />
I could make fireball of different sizes, but to do that, I should construct multiple instances of fireball each containing different size info</p>
<pre>
[source lang=&quot;cpp&quot;]character.doSkill(new Fireball(3)) //3 is the size[/source]</pre>
Edited by lride

Share this post


Link to post
Share on other sites
[quote name='slayemin' timestamp='1352137568' post='4997642']
The main reason singletons are bad is because they're pretty much like publicly accessible global variables.
...
Just.... entirely forget what a singleton is. Erase it from memory.
[/quote]

I often see this statement, but I don't understand it, so please help me understand. That is, I believe I understand the problem of using global variables.

A problem with types is that they are (usually) global. That is, if there is a class defined in a header file, someone can include that header file and instantiate an object from it. This could lead to errors if the class manages a resource that there only should be one instance of. Using the singleton design pattern will guarantee that there can only be one instance.

Share this post


Link to post
Share on other sites
Whats wrong with creating a Fireball instance and passing a const ref or pointer?

game.init(){
.....
.....
fireball= new Fireball()
character= new Character();
}

game.iteration(){
.....
.....
character.doSkill( fireball)
}

Share this post


Link to post
Share on other sites
If you're okay with pre-loading all the resources associated with all the skills then why not just store them as resources with some kind of reference table and then create a skill class that manages any and all skills by referencing the table?

....

Now that I'm thinking about it, what are you actually talking about? Can you post or explain one of these classes? Are these classes that describe the skill's attributes or level, or are these classes that execute the skill? Edited by Khatharr

Share this post


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

  • Advertisement