Wednesday 2 July 2014

Singleton design pattern Implementation

Singleton design pattern


JMS                                                                                                                            Java-Collections

As we know, Singleton design pattern is a creational design pattern. The purpose of this design pattern is to control object creation.

The main thing about this pattern is :-
  • This pattern restricts the instantiation of the class and ensures that only one instance should be available in JVM.
  • The another criteria is this instance should be globally accessed.


As per GOF:- “Ensure a class has only one instance, and provide a global point of access to it.

The main thing about this pattern is the implementation. Implementation example:-

package com.gaurav.designpattern;

public class SingletonDesignPatternDemo {
private static SingletonDesignPatternDemo singletonDesignPatternDemo;

private SingletonDesignPatternDemo() {
}

public static SingletonDesignPatternDemo getSingleInstance() {
if (singletonDesignPatternDemo == null) {
synchronized (SingletonDesignPatternDemo.class) {
if (singletonDesignPatternDemo == null) {
singletonDesignPatternDemo = new SingletonDesignPatternDemo();
}
}
}
return singletonDesignPatternDemo;
}
}

For more information on Implementation of Singleton design pattern check below link:-


Java Version & Its Features                  Design Patterns                                               Core Java

No comments:

Post a Comment