How Singleton pattern in Java

image0

What is singleton ..?

It is a Design pattern. At this time what is Design pattern ..? To find out the best way to do a thing apart from the various method. Documenting a solution for the common problems.

The Rule for the Singleton are:

  • restrict the creating the object of the class
  • only one object is created over the application
  • If multiple thread accessed then thread safe

How to restrict the creating object..?

Define the access modifier of the constructor be Private and add the final keyword to the class then only the class can't be extended.

Then How to create a one instance and access ..?

Create a instance with in the same class and accessed using the static method.

Here is the Example code:

public final class SingletonExample {

  private static SingletonExample singleton = new SingletonExample();

  private SingletonExample() {
    // Write your Functionality Here
  }

  public static SingletonExample getInstance() {
    return singleton;
  }
}

Usage: save the below as Arul.java

class Foo {
  public static SingletonExample obj;
  public static void method() {
    obj = SingletonExample.getInstance();
  }
}

class Arul{
  public static void main(String arg[]) {
    SingletonExample obj = SingletonExample.getInstance();
    Foo.method();
    if (Foo.obj == obj) {
      System.out.println("Both the objects are same ");
    } else {
      System.out.println("Not the same ");
    }
  }
}

In the Both Foo and Arul class access the same instance.

image1

For more : http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29

Show Comments