Home > DeveloperSection > Blogs > JAVA : Singleton Pattern

JAVA : Singleton Pattern


Java Java 
Ratings:
0 Comment(s)
 857  View(s)
Rate this:

Singleton Pattern

 

Previously we seen how to  create Dynamic Objective Test Application through Swing in JAVA : Creating Dynamic Objective Test Application through Swing in JAVA ( Using MySQL Database )

 

The Singleton's purpose is to control object creation, limiting the number of objects to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.

 

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

 

 

Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and file systems are prototypical examples. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access. ]

  

Singleton Design Pattern Diagram:



The Singleton design pattern addresses all of these concerns. With the Singleton design pattern you can:

          Ensure that only one instance of a class is created

          Provide a global point of access to the object

          Allow multiple instances in the future without affecting a singleton class's clients



Singleton Pattern: Code

 

public class ClassicSingleton {

   private static ClassicSingleton instance = null;

   protected ClassicSingleton() {

      // Exists only to defeat instantiation.

   }

   public static ClassicSingleton getInstance() {

      if(instance == null) {

         instance = new ClassicSingleton();

      }

      return instance;

   }

}

   The ClassicSingleton class maintains a static reference to the one singleton instance and returns that reference from the static getInstance() method.

    ClassicSingleton employs a technique known as lazy instantiation to create the singleton; as a result, the singleton instance is not created until the getInstance() method is called for the first time. This technique ensures that singleton instances are created only when needed.

  Notice that ClassicSingleton implements a protected constructor so clients cannot instantiate ClassicSingleton instances;

 

For completeness, here is how a client can access the Singleton:

//access the singleton

Singleton singleton = Singleton.getInstance();

//use the singleton

 

Watch Out for the Downsides:

 

An important consideration is multi-threading. If two threads call the getInstance() method at the same time, you can end up with two singletons. Making the getInstance() method synchronised solves this issue, but then you have the performance cost - calling a synchronised version of getInstance() will be slower than the non-synchronized version.  However, maybe the best way around this is to sacrifice lazy-loading in the Singleton, ensuring there can only ever be on instance.

 

 

Next, we will learn about : JAVA - Factory Pattern

 


Don't want to miss updates? Please click the below button!

Follow MindStick