Purpose of singletons in programming

后端 未结 9 1591
傲寒
傲寒 2020-12-08 01:49

This is admittedly a rather loose question. My current understanding of singletons is that they are a class that you set up in such a way that only one instance is ever crea

相关标签:
9条回答
  • 2020-12-08 02:20

    The main advantage of a singleton over a class consisting of statics is that you can later easily decide that you need in fact more than one instance, e.g. one per thread.

    However, in practice the main purpose of singletons is to make people feel less bad about having global variables.

    A practical example for a good use of a singleton: you have an app that uses an SQL database and you need a connection pool. The purpose of such a pool is to reuse DB connection, so you definitely want all clients to use the same pool. Thus, having it as a singleton is the correct design. But one day you need the app to connect to a second DB server, and realize that you cannot have connections to different servers in the same pool. Thus your "one instance overall" singleton becomes "one instance per DB server".

    0 讨论(0)
  • 2020-12-08 02:22

    Not all languages have "static classes" (for example C++ doesn't have them).

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

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

    0 讨论(0)
  • 2020-12-08 02:25

    Like others have said:

    • Singletons are global variables by another name.
    • Singletons are usually a bad idea.
    • Singletons could be replaced by "monostate" classes - classes that have apparently normal construction / destruction semantics but all share the same state.

    Note that in my opinion "static classes" are usually also a bad idea, a hackish workaround for a language that does not allow free functions, or for sharing state between a bunch of functions without wanting to pass that state as a parameter.

    In my experience nearly all designs with singletons or static classes can be turned into something better, more easily understood and more flexible by getting rid of those constructs.

    Edit: By request, why most singletons are global variables by another name.

    In most of the languages I know, most singleton classes are accessed through a static member function of that class. The single instance is available to all code that has access to the definition of the singleton class. This is a global variable - all code that includes the class could be making modifications to the single instance of your singleton.
    If you do not use the static member function (or some static factory method which has the same implications), but instead pass the singleton object to all clients that need it, then you would have no need for the singleton pattern, just pass the same object to all clients.

    0 讨论(0)
  • 2020-12-08 02:31

    A little knowledge is a dangerous thing and Singletons are dangerous entities. In addition to written things above, I can emphasize the life-time management of Singleton objects are also important. In ACE framework, it is handled successfully. You can find the paper here: http://www.cs.wustl.edu/~schmidt/PDF/ObjMan.pdf

    Please also note that singletons should be non-copyable classes. This pattern may seem to be the easiest one, but, on the contrary it is one of the difficult. Therefore, I ask to candidates about this evil points in Singletons.

    0 讨论(0)
  • 2020-12-08 02:33
    1. Singleton is a very useful replacement of global variables, used all across the code.
    2. Singletons are usually not "new"ed or "delete"d, they tend to be initialized on first use and deleted along with program scope
    3. Singletons perfectly match for wrapping logging, configuration and other hardware-interfacing classes.
    0 讨论(0)
  • 2020-12-08 02:41

    There's two ways to use singletons.

    1. The way they should be used. Typically with immutable variables (C#'s String.Empty, classes in Smalltalk, etc.). This is approximately 1% of singleton usage.
    2. As a replacement for global variables. This is bad. The root cause of this is people that want to share common objects without understanding how to properly use a Builder. Use of Singletons in this fashion is typically a sign of a lack of deep understanding of object-oriented design.
    0 讨论(0)
提交回复
热议问题