7

Possible Duplicate:
C++ singleton vs completely static object

Hi,

why should I prefer a singleton over static class methods.

    MoneyPrinter::addJob(PrinterJob &job);
or
    MoneyPrinter::getInstance().addJob(PrinterJob &job);

Is it only a matter of style? What do you use? Why?

ps. I know that sigletons are not threadsafe (first initialization) by default.

Community
  • 1
  • 1
c0de
  • 1,132
  • 1
  • 10
  • 10

4 Answers4

5

why should I prefer a singleton over static class methods

A singleton can have internal state (in your example, the list of added jobs), i.e. the member data of the singleton class.

What do you use? Why?

If there's no state, then a static method because that's simplest.

Otherwise a singleton, preferably static-initialized (instead of just-in-time or run-time initialized).

ChrisW
  • 54,973
  • 13
  • 116
  • 224
  • 8
    You could have state with static methods, it would just be static variables. – Billy ONeal Nov 28 '10 at 22:12
  • 2
    If there is no state, why make an class with static methods only that will never be instanciated *in a language that doesn't have a everything-must-be-in-a-class-fetish*? Just make it a bunch of functions, perhaps in an appropriate namespace. –  Nov 28 '10 at 22:16
  • @Billy ONeal - If there are static variables to be instantiated, then they're similar to a singleton and I'd prefer to group them together within the singleton class – ChrisW Nov 28 '10 at 22:17
  • 1
    @delnan: I agree with you. My point is merely that such a thing is possible. @Chris: What's the difference between that and simply having static fields on your class? – Billy ONeal Nov 28 '10 at 22:18
  • 1
    @Billy ONeal - Making them member data ensures they're all constructed at approximately the same time (when the singleton is instantiated). Another advatnage of a singleton is that it can be super/subclass whose exact type isn't defined until it's instantiated. – ChrisW Nov 28 '10 at 22:25
  • @ChrisW: Fair enough. +1 – Billy ONeal Nov 28 '10 at 22:28
3

A singleton gives you control over when the class is instantiated and as DeadMG points out if you want to instantiate it. A static class is less controllable and is instantiated before main is called.

The sequence in which classes is instantiated can sometimes be critical when the singleton depends on some other class or resource which is not avaiable before main is called.

As you mentioned, if you call a singleton from multiple threads you need to ensure that you have used a thread safe singleton. Scott Meyer's (from Effective C++) is not thread safe for example.

T33C
  • 4,341
  • 2
  • 20
  • 42
  • 1
    You can always manipulate any secret, static data stuff within the implementation file of the static "Singleton". In fact, you could use a complete class structure and then the difference between any of the various singletons and this static thing is purely syntatic. – Edward Strange Nov 28 '10 at 22:34
  • @Noah - I would say the differences are also semantic and that giowck has asked a good question because both approaches should be considered and understood. – T33C Nov 28 '10 at 22:38
2

Would be harder to refactor out the Singleton-ness simply because of the syntax used if you use static member functions. There's one reason.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
1

The general rule of singletons is, if you have to ask, don't use it. This is true for any globally mutable state.

Puppy
  • 144,682
  • 38
  • 256
  • 465