Sign in to follow this  
X Abstract X

Why are Singletons so Complex?

Recommended Posts

X Abstract X    109
I've been searching around learning about designing Singletons and of course I found that they are extremely complex. No perfect solution exists, as far as I could find. I'm just curious, if I have the following class:
class Singleton {

private:
    Singleton() {}
    ~Singleton() {}
    Singleton(const Singleton&) {}

public:
    static void printSomeText();

};


Is this a singleton? Is there any problems with this very basic design?

Share this post


Link to post
Share on other sites
jyk    2094
No, that's not a singleton. They're not really that complicated though - if you look around online a bit, you should be able to find some examples of how to implement them.

However, I suggest you check out this list of singleton-related threads compiled by Promit (second post down) before going much further down this road.

Share this post


Link to post
Share on other sites
Wavarian    850
I wouldn't call that a singleton.. Maybe a bastardisation of functional programming..

If you're going to have a class full of static functions then you might has well use a namespace instead of a class. You can't even pass around an instance of your Singleton class as you could if it were a real singleton:



class Singleton
{

private:

// Constructor
Singleton()
{

}

// Copy constructor - not implemented
Singleton(const Singleton&);

// Assignment operator - not implemented
const Singleton& operator =(const Singleton&);

public:

// Destructor
~Singleton()
{

}

// Returns the one and only Singleton instance
static Singleton& GetInstance()
{
static Singleton INSTANCE;

return INSTANCE;
}

// Some method
void DoStuff(int value) const
{

}
};




..And the above class could be used like this:



void AnotherFunction(const Singleton& singleton)
{
singleton.DoStuff(6);
}

int main()
{
Singleton& singleton = Singleton::GetInstance();

singleton.DoStuff(5);

AnotherFunction(singleton);

return 0;
}




But if you don't know what a singleton is or how it works you probably don't need one.

Share this post


Link to post
Share on other sites
X Abstract X    109
Quote:
Original post by Wavarian
But if you don't know what a singleton is or how it works you probably don't need one.


Well, I need to write a console sort of feature to report errors and other events for my entire application. Is this a good candidate?

Share this post


Link to post
Share on other sites
Washu    7829
Quote:
Original post by X Abstract X
Quote:
Original post by Wavarian
But if you don't know what a singleton is or how it works you probably don't need one.


Well, I need to write a console sort of feature to report errors and other events for my entire application. Is this a good candidate?


No.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Well, I need to write a console sort of feature to report errors and other events for my entire application. Is this a good candidate?
I'd suggest reading some of those threads/articles I linked to. Also, it sounds like you're wanting to implement a logger, and there have been quite a few threads on that topic as well (a forum search for 'logger' would probably lead you to some of these threads).

As for your question though, if you want global access to your logger, I would just use a global object, function, or set of functions (again though, check out some of the previous threads on the topic for ideas on how a logger can be implemented).

Share this post


Link to post
Share on other sites
code_zombie    165
Quote:
Original post by X Abstract X
Quote:
Original post by Wavarian
But if you don't know what a singleton is or how it works you probably don't need one.


Well, I need to write a console sort of feature to report errors and other events for my entire application. Is this a good candidate?


There is an output stream already available in C++ that you can use for logging. Look into std::clog. You can connect its output to the console, a file, or any other output stream.

Share this post


Link to post
Share on other sites
Fenrisulvur    186
tbh I've never really encountered a real anti-patternesque example of a singleton, but indeed they're not often terribly relevant.

Examples of where I'd use them would include if I wanted a lazy-evaluated list of something that's going to look the same every time it's generated, such as a list of prime numbers. There's really no point in generating the same hundred million primes fifty times, so you feed everything the same object and save the clock cycles.

Mind, if you can't imagine your singleton instance ever being used by a generic* routine in place of an object of some related type**, then as has been said, you're probably best off using a namespace or a class of static methods or whatever.

*Generic as in general-purpose, not as in templates, type params et al.

**In the case of the list of prime numbers example, I don't usually find it helpful to the actual mathematics - you normally use prime numbers in methods that need prime numbers - but it can help with visualization and statistical analysis.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this