Singleton pattern

The Singleton (rarely also called single piece) is employed in the software development design patterns and belongs to the category of generation pattern. It ensures that a class there is exactly one object. This Singleton beyond usually is globally available. The pattern is one of the so-called Gang of Four ( GoF ) published patterns.

Use

Singleton is used when

  • Only one object may be available at a class and a simple protocol needed to access this object or
  • The only object is to be specialized by subclassing.

Examples of applications are

  • A key protocol - object, which writes output to a file.
  • Print jobs that are sent to a printer should be written in a single buffer.

UML diagram

Properties

The single piece (Singleton )

  • Created and managed the only object of the class
  • Provides global access to this object through an instance operation ( getInstance ()).

It is

  • The instance operation is a class method that is statically bound
  • The private attribute "instance" (instance ) a class attribute, ie a static attribute.

In parentheses are the numbers from the picture above.

Benefits

The pattern provides an improvement over the global variables:

  • Access control can be realized.
  • The singleton can be specialized by subclassing.
  • What subclass should be used can be decided at run time.
  • The single instance must be generated only when it is needed.
  • If later, several objects are required, a change is easier than global variables.

Disadvantages

  • There is a great risk, quasi implement by excessive use of singletons an equivalent to global variables and so then procedural rather than object oriented programming.
  • Relationship to the singleton class be disguised, that is, whether a singleton class is used, not obvious from the interface of a class, but only on the basis of the implementation. In addition, the coupling is increased, which restricts reusability and clarity.
  • The "scope " of singletons, ie the area in which a singleton really technically "single" is, does not have to coincide with the area in which it is to be "single". Specifically, in concurrent or even distributed systems difficult to ensure that there is really only one instance. For example, in Java, a simple static variable " individually per classloader "; in distributed (for example, cluster-aware ) systems or complex concurrent applications such as application servers can free up coexist multiple instances.
  • In systems with parallel processes (threads) must be ensured that does not exist in the short term more than one instance by parallel initialization; and that the singleton object later use in many parallel processes allowed, so is thread safe.
  • If dynamic libraries used sometimes has to be ensured that only one instance can exist here.

Because of the many disadvantages of the Singleton pattern ( and also the idiom Double -checked locking ) is sometimes already rated as anti-pattern. For cases in which is actually technically a suitable range for a singleton exists, but singletons can be useful - especially if they, for example, relate to other " unique structures " as a Abstract Factory. Nevertheless, the correct design of singletons is difficult - usually more difficult than designs without singletons.

Use in the analysis

In the analysis, a (discipline ) Singleton is usually characterized by the fact that the multiplicity of the class is defined as 1. As in the design of the range of multiplicity must be questioned: Are there really only "a central location for ...", or, for example, in transnational systems very well several objects of one type exist?

Implementation

From lazy creation is when the only object of the class is not created until it is needed. The aim is that the memory requirements and processing time are spent only for the instantiation of the object when the object is actually needed. To this end, the constructor is called only on the first call of the function getInstance ().

With appropriate means lazy creation Singleton implementations can be sure of concurrency by the central method getInstance is marked for example in Java synchronized with the keyword. However, a simpler alternative is the possibility, already generate the Singleton during the initialization of the class, the access method is then only needs to return. Therefore, they need not be synchronized, making access somewhat accelerated. This process is also known as eager creation ( German " eager generation ").

If a singleton must not be derived from another class, you can simply declare the class as static - This is still the singleton principle. These so-called mono - State classes ( called in Python also Borg pattern ) were obtained from S. Ball and J. Crawford in her article " mono State classes: the power of one" suggested. Here, any number of instances of a class exist, but they share a common state. In C , Java and PHP, this can easily be realized by all class attributes are declared as static. The name Borg comes from a post by Alex Martelli in ASPN and refers to Star Trek.

In programming languages ​​which support enums ( such as Java version 5 or higher ) can be used to implement the singleton pattern, the enum language construct can be used, which also allows direct serializability. However, in order derivative of singletons from another class is also impossible.

If you want a certain number of instances of a class ( multi-tone ), so you can just add more static public instance fields ( public static readonly Singleton = new Singleton Instance2 () ;).

An alternative, which creates the instance until the first call to getInstance (), is known under the name of the initialization on demand holder idiom. The instance is created in an inner class Holder, which is not loaded until the first call to getInstance (), and not when the class Singleton store.

For code examples, see the list of singleton implementations.

Related Design Patterns

The characteristics of singleton meet for many classes of the other patterns, so that they are executed as a singleton.

For example, abstract factories, builders ( design patterns ) or prototypes often are singleton.

732204
de