17

Isn’t a class with only static members a kind of singleton design pattern? Is there any disadvantage of having such a class? A detailed explanation would help.

Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
Shree
  • 4,627
  • 6
  • 37
  • 49

5 Answers5

18

This kind of class is known as a monostate - it is somewhat different from a singleton.

Why use a monostate rather than a singleton? In their original paper on the pattern, Bell & Crawford suggest three reasonns (paraphrased by me):

  • More natural access syntax
  • singleton lacks a name
  • easier to inherit from

I must admit, I don't find any of these particularly compelling. On the other hand, the monostate is definitely no worse than the singleton.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • Different yes, but doesn't it achieve exactly the same thing? I can't see how this has any advantages or disadvantages over the "traditional" singleton, in C++ at least. – j_random_hacker Apr 06 '09 at 11:06
  • 1
    There is the difference that space will not be allocated for a singleton unless it is used, but in practice a singleton object that is never used seems like a rare case. Does anyone have a common use-case where this would be important? – j_random_hacker Apr 06 '09 at 11:10
  • I also tend to think that the lazy-construction feature of typical singletons is useless in most cases -- those where we just need a global variable. However, in library case, we may need an on-the-fly construction in order to simplify the end-user's (developer actually) code. – Luc Hermitte Apr 06 '09 at 12:15
  • First link is broken. Perhaps update with this one? http://wiki.c2.com/?MonostatePattern – Sam Texas Apr 03 '17 at 09:02
15

Robert C. Martin wrote an article some times ago about the differences between the mono state pattern and the singleton pattern.

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • 1
    Based on the article, the only material differences that I could see were that in Java, destruction of a Monostate is guaranteed but it cannot be passed between JVMs -- are there any differences relevant to C++? – j_random_hacker Apr 06 '09 at 11:08
  • It's exactly the same as with other variables. If it is dynamically allocated, it should be deallocated with a compatible deallocator. If it is in the static storage (see GOTW/XC++, IIRC), it will be destroyed and the end of the program, but there is no way to implement dependencies. – Luc Hermitte Apr 06 '09 at 12:10
  • Regarding the dependencies aspect, A.Alexandrescu's loki (from MC++D) singletons, and ACE singletons permit to specify dependencies. BTW, they are both non intrusive singletons. – Luc Hermitte Apr 06 '09 at 12:16
2

Consider a family of Logging classes. They all implement "LogMessage(message, file, line_number). Some send messages to stderr, some send email to a set of developers, some increment the count of the particular message in a message-frequency table, some route to /dev/null. At runtime, the program checks its argument vector, registry, or environment variables for which Logging technique to use and instantiates the Logging Singleton with an object from a suitable class, possibly loading an end-user-supplied DLL to do so. That functionality is tough to duplicate with a pure static Singleton.

Thomas L Holaday
  • 13,614
  • 6
  • 40
  • 51
1

For a singleton all constructors have to be private, so that you can access only through a function. But you're pretty close to it.

DaClown
  • 4,171
  • 6
  • 31
  • 31
  • If you really think that you should read about singletons again. Private constructors and one or more static members only accessilbe through a public member function is the very definition of a singleton – DaClown Apr 06 '09 at 13:37
  • 1
    If this answer is so wrong that it deserves 5 downvotes, could you leave a comment why? Something like 'wrong wrong wrong' is not helpful and I'm still not convinced that I'm wrong. – DaClown Apr 07 '09 at 11:16
1

class with all static members/methods a kind of singleton design pattern

Class - not pattern. When we talk about classes we can say class implements pattern.


Static functions - is not member functions, they are similar on global functions. Maybe you don't need any class?

Quote from wikipedia:

In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.

By this definition your implementation is not singleton implementation - you don't use common idea One (or several in extended definition) instance of class.

But sometimes (not always) usage of class with all static functions and singleton pattern - not have meaningful difference.

bayda
  • 13,365
  • 8
  • 39
  • 48