Are there any cases when a singleton is the best option?

There are many discussions on this site about why single games should be avoided, but are there any cases where a singleton makes sense and is the best option?

An example where I believe that one single is the best option is to implement an Identity Map , where you need to track all the models that are already loaded from the database in your display layer. In this case, it is important that the card is global, so that all client codes use the same instance of the card, and they all gain access to the same list of loaded models. The simplest solution seems to be to implement the class as a singleton (or as static methods). Is this a better approach or is there another way?

Are there any other cases where you can use a singleton or are they always bad?

+2
oop design-patterns singleton
Jan 16 '09 at 8:50
source share
8 answers

Watch this stream .

+1
Jan 16 '09 at 9:02
source share

Singleton really use them. I would generalize their usefulness as:

"A singleton should be used to represent an object, in accordance with the fundamental design or requirements, there can be no more than one instance, and there is data or resources associated with the object that carries a measurable cost." Here is what I mean.

1) You should not use Singleton in the case when there is a lot of something, but we are only interested in one. If there can be two on the system, create the instance explicitly.

2) Many applications of Singleton can be replaced by a class with all static methods and data. If you can do it without a performance penalty, do it.

3) You should consider Singleton if there is a noticeable cost for setting up the object: for example, if it is a physical resource that needs to be initialized, or if it relies on some lookup table that needs to be initialized. If so, using Singleton you can defer initialization costs until the first use of the object, while a static class will usually be initialized when the application starts. If the object is never used, you never pay for initialization.

+1
Jan 21 '09 at 10:15
source share

Singletones should be used when you need only one instance , and you want to ensure its implementation through design. It should not be used due to a global side effect .

In your case, I think it makes sense because you want to limit only one instance of the class.

+1
Feb 19 '09 at 22:49
source share

There is a use for singleons (and the like) in languages ​​that do not allow static interface members, such as in .NET. By providing singleton, you have an instance that can provide an interface as an instance. Therefore, in .NET examples can be:

  • Comparer<T>.Default - provides an IComparer<T> implementation for sorting T
  • EqualityComparer<T>.Default - provides an implementation of IEqualityComparer<T> for hashing / equality-testing T

It is a means to an end; this is not what we are trying to do, just a reasonable “how”.

As for the “ID card” example, I personally would prefer to keep this in an instance and make the instance available to all my code. In addition, it helps testing if you can refuse a card between tests or have the flexibility (later) to have several identification cards at the same time.

0
Jan 16 '09 at 8:54
source share

Think about where you want to log various events in the application in a single file. In this case, a single-level Log class will be useful. Because the other part of the application will access one instance of the log class.

0
Jan 16 '09 at 8:55
source share

Singleton has its own use. They also have their own abuses, and there are many reasons they do not like them or why they make testing difficult.

Here's a little protection for the Singleton template.

Sometimes a program must present or interact with something truly unique. An example is a physical resource. For example, you cannot have more than one "stdout". And a representation of the OS process (e.g. J2ME Midlet).

Singlets, like using a class / interface to provide encapsulation, and such advantages for what would otherwise be a bare global variable, are worth it.

0
Jan 16 '09 at 9:18
source share

Singletones make sense when you use web programming: usually you only need one instance of the page output class for each request and one user object for it. Databases are different, although implementation of separation of powers is a good reason for wanting more than one db object.

0
Jan 16 '09 at 9:45
source share

My personal guide is never to hard code a class as a singleton if you can instantiate an instance of a single class object. This makes singletones very rare and often unnecessary in application code. This is especially true when using DI frameworks such as Spring.

Singletones are really used as “entry points” in frameworks, but the rule there will be to use them as “root singletones” (at the beginning of the dependency hierarchy), and the rest of the code depends on this singleton as little as possible.

0
Jan 16 '09 at 10:13
source share



All Articles