Singleton Design Pattern
Posted on March 20, 2023 by Kunal Saxena
Design Patterns
Singleton
When there is only one instance creation is allowed
OR
We want to restrict instance creation of particular class and allow only one instance.
In such scenarios, we use Singleton Design Pattern. I have explained Singleton design pattern in detail in video below:-
Source Code is given below:-
When there is only one instance creation is allowed
OR
We want to restrict instance creation of particular class and allow only one instance.
In such scenarios, we use Singleton Design Pattern. I have explained Singleton design pattern in detail in video below:-
Source Code is given below:-
package com.techiekunal.codepractice.designpatterns;
/**
* Basic Singleton Example with Eager initialization
*
* @author kunalsaxena
*
*/
public class SingletonEagerExample {
private static final SingletonEagerExample INSTANCE = new SingletonEagerExample();
// prevent instamce creation from outside
private SingletonEagerExample() {
}
/**
* public method to get instance
* static so that it can be accessed without object.
* outside world will not be able to create object
*
* @return
*/
public static SingletonEagerExample getInstance() {
return INSTANCE;
}
}