-
Notifications
You must be signed in to change notification settings - Fork 4
/
DbSingleton.java
34 lines (29 loc) · 1 KB
/
DbSingleton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package gof_design_patterns_a_creational.p01_singleton;
/*
* The primitive Singleton example class
* Notify that instance is stored in a static reference and it is not thread safe.
*
* Also the instance is not Lazy Loaded but Eagerly Loaded so the instance is instantiated
* as the JVM starts.
*/
public class DbSingleton {
/*
* Instance is the reference to the unique static Singleton class instance.
* The instance is eagerly loaded which means, it is created as the JVM starts.
*/
private static DbSingleton instance = new DbSingleton();
/*
* The private constructor of the Singleton
* We want nobody can call the constructor outside, clients are only allowed
* to get the unique instance via the public getInstance method
*/
private DbSingleton() {
}
/*
* With only the static getInstance method, the client can reach to the unique instance
* but cannot modify the private instance reference inside the class
*/
public static DbSingleton getInstance() {
return instance;
}
}