Answers for "singleton design pattern in java"

5

single design pattern

Singleton Design Pattern is basically limiting our class so that 
whoever is using that class can only create 1 instance from that class.
       
I create a private constructor which limits access to the instance of the class.
Than I create a getter method where we specify how to create&use the instance.
    - I am using JAVA Encapsulation OOP concept.
Posted by: Guest on December-04-2020
0

threadsafe singleton pattern in java

package com.journaldev.designpatterns;

public class ASingleton {

	private static volatile ASingleton instance;
	private static Object mutex = new Object();

	private ASingleton() {
	}

	public static ASingleton getInstance() {
		ASingleton result = instance;
		if (result == null) {
			synchronized (mutex) {
				result = instance;
				if (result == null)
					instance = result = new ASingleton();
			}
		}
		return result;
	}

}
Posted by: Guest on June-16-2020

Code answers related to "singleton design pattern in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language