Singleton Design Pattern

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists.

public class Singleton {
	private static Singleton instance;
	private Singleton() {}
	public static Singleton getInstance() {
		if (instance == null) {
			instance = new Singleton();
		return instance;

The above code is the general class definition of Singleton Design Pattern.

The above code guarantees the single object creation of the class. The only way to retrieve an instance of the class is to call getInstance() method.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *