mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 12:58:37 +00:00
New Singleton class
Thread-safe and lazy loading.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class SingletonClass {
|
||||
|
||||
private static SingletonClass singletonInstance = null;
|
||||
|
||||
public synchronized static SingletonClass getSingleton() {
|
||||
/*
|
||||
* The instance gets created only when it is called for first time.
|
||||
* Lazy-loading
|
||||
*/
|
||||
if (singletonInstance == null) {
|
||||
singletonInstance = new SingletonClass();
|
||||
}
|
||||
|
||||
return singletonInstance;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user