Singleton – A Single teether for everyone

In Singleton design pattern, a java program streamlines the instantiation of a certain class to a single object. This object acts as a controller for the whole system or of a subsystem (If you are programming a program in multi-pattern oriented way). Let’s discuss some properties, examples, benefits, issues, and unintended behaviours in more detail.

Singleton Properties

Following are of some of the very important properties of the singleton design pattern:

  • A class must have a single Object. No additional instantiation is allowed.
  • The singleton class object is globally accessible (think of a static object)
  • Certain functions can only be delegated through a single object e.g. you might have a single factory in your system which creates new products
  • The accessibility ensures ease, and continuous availability (more on this coming later)
  • There is a mechanism to ensure that no other instance is available
  • Ensures controlled access i.e. ensures that a single object should be allowed to use the singleton object at one particular time. Hence it also promotes the atomicity in operations
  • It can be used for sub-classing cases and non-subclassing cases of the same class, to creates enhanced singletons within a single singleton
  • Other objects get instance reference from the same singleton object using static method
  • ‘Just-in-time initialization’ OR ‘Initialization on First Use’
Singleton – An example class

Let’s see an example Singleton class in Java

Here is a test program which tests our singleton object

Singleton – Sub-class case

You can subclass a Singleton class itself and it has its own uses. Let’s see you make two sub-classes of a singleton class. The single instance that is available throughout the lifetime of a client code would be an instance of one of the subclass type. So in a single system, you can have multiple subclasses but only one instance of any one of the subclasses at one particular time. You can pass the name of the subclass (as class name or a string value) to the mother singleton to create a subclass instance for you. You would have a single static instance of the mother (parent class but I just saw Star Trek again and mothership is still on my mind) class, which will decide which subclass to instantiate.

Singleton – A sub-class example

Here is an example Java code:

Here is an example of the classes system, in a specific order (also remember to create your subclasses, which I am not showing here)

Usage by other design patterns

Design patterns like Abstract Factory, Prototype, and Builder can use Singleton design pattern for their own implementation. Similarly, for the facade objects, you can use singleton design pattern because in the whole system you need a single Facade object. If you want to replace global variables then Singleton SHOULD not be your obvious choice. Many people mix the concept of global variables with the singleton and try to create a lot of singleton behaviours where global variables would have suffice. A singleton itself is a global variable (I hope you get the point).

Synchronization Issues (multi-threading) & and Breakage

A client code with multiple threads can try to get the instance from the Singleton class at the same time. The above example code will FAIL in a multi-threaded environment. I will discuss multi-threading and its effects on Singleton in a later post.