MCPcopy Create free account
hub / github.com/Anchals24/Low-Level-Design / Singleton

Class Singleton

SingletonDesignPattern/Singleton.java:3–22  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1package CreationalDesignPattern.SingletonDesignPattern;
2
3public class Singleton {
4 //Step1: We have created a private constructor of Singleton class & it can only be instantiated within this class.
5 private Singleton(){
6 }
7
8 //Step2: Create a static var to hold one instance of Singleton class.
9
10 private static Singleton uniqueInstance;
11
12 //Step3: Create a static getInstance() method to instantiate the class and also to return an instance of it.
13
14 public static Singleton getInstance(){
15 if (uniqueInstance == null){
16 uniqueInstance = new Singleton();
17 }
18 return uniqueInstance;
19 }
20
21 //other useful methods here.
22}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected