The purpose of singleton programming

This is admittedly a pretty free question. My current understanding of singletones is that they are a class that you created in such a way that only one instance is ever created.

This is very similar to a static class for me. The main difference is that with a static class you cannot / cannot use it, you just use it, for example, Math.pi() . With the singleton class, you still need to do something like

 singleton mySingleton = new singleton(); mysingleton.set_name("foo"); singleton otherSingleton = new singleton(); // correct me if i am wrong, but mysingleton == othersingleton right now, yes? // this the following should happen? otherSingleston.set_name("bar"); mysingleton.report_name(); // will output "bar" won't it? 

Please note that I ask this language myself, more about the concept. So I'm not worried about how to code such a class, but even more so, why you don’t want and what you will need to consider.

+46
language-agnostic singleton theory
Mar 31 '10 at 7:23
source share
9 answers

The main advantage of a single element over a class consisting of statics is that later you can easily decide that you actually need more than one instance, for example. one per stream.

In practice, however, the main goal of singletons is to make people feel less bad about global variables.

A practical example for good use of singleton: you have an application that uses an SQL database and you need a connection pool. The purpose of such a pool is to reuse the database connection, so you definitely want all clients to use the same pool. Thus, having as a singleton code is the right design. But one day you will need an application to connect to a second database server and make sure that you cannot connect to different servers in the same pool. Thus, your singleton “single instance” becomes “single instance on the database server”.

+50
Mar 31 '10 at 7:31
source share

why don't you want

I would not because singlets are usually a very bad way to solve your problems. My recommendation to you is to completely avoid them.

Main reasons:

  • The singletones basically represent a global state (this is evil).
  • Proper injection of dependencies becomes impossible.

I suggest you read the rest (including detailed explanations) on this Google employee blog:

+11
Mar 31 '10 at 7:32
source share

Singletones are mostly useful when you need an interface for a singleton service, but you don't know until an instance of a particular class is created.

For example, you might want to declare a central journaling service, but only at runtime decide whether to connect a file logger, dummy logger, database logger, or message queue logger.

+2
Mar 31 '10 at 7:27
source share

As others said:

  • Singletones are global variables with a different name.
  • Singletones are usually a bad idea.
  • Singletones can be replaced with "monostate" classes - classes that seem to have the usual build / destroy semantics, but all have the same state.

Please note that, in my opinion, “static classes” are usually also a bad idea, a hacker workaround for a language that does not allow free functions, or for sharing a state between many functions, not wanting to pass this state as a parameter.

In my experience, almost all projects with single or static classes can be turned into something better, more understandable and flexible, getting rid of these designs.

Edit: on request why most singletons are global variables with a different name.

In most languages ​​that I know, most singleton classes are accessible through a static member function of this class. A single instance is available for all code that has access to the definition of the singleton class. This is a global variable - all the code that the class includes can make changes to one instance of your singlet.
If you are not using a static member function (or some static factory method that has the same consequences), but instead pass the singleton object to all the clients that need it, then you won't need the singleton template, just execute the same object for all clients.

+2
Mar 31 '10 at 7:42
source share

A little knowledge is a dangerous thing, and singletones are dangerous creatures. In addition to the above, I can emphasize that managing the life cycle of Singleton objects is also important. In an ACE environment, it is successfully processed. You can find the article here: http://www.cs.wustl.edu/~schmidt/PDF/ObjMan.pdf

Also note that single player games must be non-copyable classes. This picture may seem the simplest, but, on the contrary, it is one of the difficult ones. Therefore, I ask the candidates about this evil at Singleton.

+2
Mar 31 '10 at 8:16
source share

In addition to the other answers, I have to say that Singletons can help you when you want a static class, but you cannot do this because, because of the design of your application, it inherits a class that is capable of action.

0
Mar 31 '10 at 8:08
source share

There are two ways to use singletons.

  • How should they be used. Usually with immutable variables (C # String.Empty, classes in Smalltalk, etc.). This is approximately 1% of singleton usage.
  • As a replacement for global variables. This is bad. The main reason for this is people who want to share common objects without understanding how to use Builder correctly. Using singleton this way is usually a sign of a lack of in-depth understanding of object-oriented design.
0
Mar 31 '10 at 8:18
source share

Not all languages ​​have “static classes” (for example, C ++ does not have them).

Again with the C ++ example, adding static variables to the class is a pain, because you need to put them both in the header and in the .cpp file, so a singleton is very useful in this case.

Each language is different. I think in C # they are not very useful (and in fact, from what I know, they are not used very often)

-one
Mar 31 '10 at 7:26
source share
  • Singleton is a very useful replacement for global variables used throughout the code.
  • Singletones are usually not "new" ed or "delete" d, they are usually initialized on first use and are deleted along with the program area
  • Singletones are ideal for handling logs, configurations, and other hardware-interface classes.
-one
Mar 31 '10 at 7:29
source share



All Articles