Tuesday, September 3, 2013

Singleton Design Pattern Example

Singleton Design Pattern: This is the most simplest and most popular design pattern. This is a type of creational design pattern. This help us in creating a single object of a class.

Multiple objects can not be created as we do for any normal class shown here.

If we have a class A then we can say

A a1 = new A();// creates a new object of A
A a2 = new A();// creates a new object of A
A a3 = new A();// creates a new object of A

Each time a new object of class A will be created so a1, a2 and a3 will refer three different instances of class A. But the same is not possible with a singleton class.
You can not create an object of Singleton class using new operator.
  Singleton singleton = new Singleton();// is not possible, compile Time Error:
  //The constructor Singleton() is not visible


No comments :

Post a Comment