Categories
Design Patterns Using Java

Singleton Design Pattern

Prerequisite: Assuming that you already know about the basics of design patterns ( DefinitionHistory, PrinciplesCatalog ) If You Are New to Design Pattern Please Read My Previous Posts.

Previous Posts Link: http://blog.btao.in/category/design-patterns-using-java/

” Now let us continue with the Singleton Design pattern 

Singleton Design Pattern

Singleton Design Pattern is a type of the creational design patterns, We Know that creational design patterns mostly deals with different object creation mechanisms, So In this Pattern We Will Study about one of the Object creation mechanism moreover It will be explained by using the Principles of design patterns…

1) Intent

Singleton is a creational design pattern that lets you ensure that a class has only one instance while providing a global access point to this instance.

2) Motivation

The government is an excellent example of the Singleton pattern. A country can have only one official government. Regardless of the personal identities of the individuals who form governments, the title, “The Government of X”, is a global point of access that identifies the group of people in charge

3) Structure

4) Participants

The Singleton class declares the static method getInstance that returns the same instance of its own class.

Singleton’s constructor should be hidden from the client code. Calling the getInstance method should be the only way of getting the Singleton object.

5) Sample Code

package singletondesignpattern;

public class Abc
{
public static Abc obj = new Abc();
private Abc()
{
}
public static Abc getInstance()
{
return obj;
}
/* 
Here the static object will be loaded when class is loaded hence object also created but the problem is even though
we not yet create an object it will be create at the time of loading the class this type of behaviour may cause 
wastage of memoy and this early creation of object type of singletonpattern is called Early Singleton Pattern this situation 
will be overcome though a lazyclass....
*/
}

6) Implementation

  • Add a private static field to the class for storing the singleton instance.
  • Declare a public static creation method for getting the singleton instance.
  • Implement “lazy initialization” inside the static method. It should create a new object on its first call and put it into the static field. The method should always return that instance on all subsequent calls.
  • Make the constructor of the class private. The static method of the class will still be able to call the constructor, but not the other objects.
  • Go over the client code and replace all direct calls to the singleton’s constructor with calls to its static creation method

7) Applicability

  • Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program.
  • Use the Singleton pattern when you need stricter control over global variables.

8) Related Patterns

  • A Facade class can often be transformed into a Singleton since a single facade object is sufficient in most cases.
  • Abstract Factories, Builders, and Prototypes can all be implemented as Singletons.

Leave a Reply

Your email address will not be published. Required fields are marked *