Skip to main content

What is singletone Design pattern in java ?

Singleton design pattern is a Creational design pattern.It means create a single object or instance and make it global ,In other words, a class must ensure that only single object should be created and single object can be used by all other classes.

There are two forms of singleton design pattern
  • Early Instantiation: creation of instance at load time.(private static A obj=new A();)
  • Lazy Instantiation: creation of instance when required.(private static A obj; )

Advantage of Singleton design pattern
  • Saves memory because object is not created at each request. Only single instance is reused again and again.

To create the singleton class, we need to have static member of class, private constructor and static factory method.
  • Static member: It gets memory only once because of static, itcontains the instance of the Singleton class.
  • Private constructor: It will prevent to instantiate the Singleton class from outside the class.
  • Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.

public class Singletone {

    private static Singletone obj; //lazy initialization

    private Singletone() {

    }

    public static Singletone getSingletone() {

        if (obj == null) {

            obj = new Singletone();// instance will be created at request time

        }
        return obj;
    }

    public void doSomething() {

        System.out.println("Singleton method called");

    }

    public static void main(String[] args) {
        Singletone ex = Singletone.getSingletone();
        ex.doSomething();
    }
}


Comments

Popular posts from this blog

Find 2nd Largest Number in java ?

import java.util.Scanner; public class SocondLargest {     public static void main(String[] args) {         int secondlargest = Integer.MIN_VALUE;         int largest = Integer.MIN_VALUE;         System.out.println(largest);         Scanner input = new Scanner(System.in);         System.out.println("Enter array values: ");         int arr[] = new int[5];         for (int i = 0; i < arr.length; i++) {             arr[i] = input.nextInt();             if (largest < arr[i]) {                 secondlargest = largest;                 largest = arr[i];  ...

Check the number is Armstrong or not ??

import java.util.Scanner; public class Armstrong { public static void main(String[] args) { System.out.println("Enter the number ..."); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int temp=n; int sum=0 ; while(n>0){  int d=n%10;  sum=sum+d*d*d;  n=n/10; } if(temp==sum){ System.out.println("armstrong"); } else{ System.out.println("not armstrong"); }       sc.close(); } }

Custom exceptional Handling in java

class InvalidAgeException extends Exception { private static final long serialVersionUID = 1L; InvalidAgeException(String s) { super(s); } } public class Custom { public static void age(int age) throws InvalidAgeException { if (age < 18) throw new InvalidAgeException("not valid"); else System.out.println("welcom to vote"); } public static void main(String[] args) { try { age(19); } catch (Exception ex) { System.out.println("exception occured " + ex); } } }