File tree Expand file tree Collapse file tree 3 files changed +26
-9
lines changed
src/main/java/com/philipjhamilton
patterns/creational/singleton Expand file tree Collapse file tree 3 files changed +26
-9
lines changed Original file line number Diff line number Diff line change 1
1
package com .philipjhamilton ;
2
2
3
- import com .philipjhamilton .patterns .creational .singleton .Singleton ;
3
+ import com .philipjhamilton .patterns .creational .singleton .SingletonLazyInit ;
4
4
5
5
/**
6
6
* Hello world!
@@ -10,7 +10,7 @@ public class App
10
10
{
11
11
public static void main ( String [] args )
12
12
{
13
- Singleton test = Singleton .getInstance ();
13
+ SingletonLazyInit test = SingletonLazyInit .getInstance ();
14
14
15
15
}
16
16
}
Original file line number Diff line number Diff line change
1
+ package com .philipjhamilton .patterns .creational .singleton ;
2
+
3
+ public enum SingletonEnum {
4
+
5
+ INSTANCE ;
6
+
7
+ int value ;
8
+
9
+ public int getValue (){
10
+ return value ;
11
+ }
12
+
13
+ public void setValue (int value ){
14
+ this .value = value ;
15
+ }
16
+
17
+ }
Original file line number Diff line number Diff line change 3
3
import java .io .Serializable ;
4
4
import java .util .HashMap ;
5
5
6
- public class Singleton implements Serializable {
6
+ public class SingletonLazyInit implements Serializable {
7
7
8
- private static volatile Singleton instance = null ;
8
+ private static volatile SingletonLazyInit instance = null ;
9
9
private static HashMap <String , String > data = new HashMap <String , String >();
10
10
11
- private Singleton (){
11
+ private SingletonLazyInit (){
12
12
if (instance != null ){
13
13
throw new RuntimeException ("Use getInstance() method to get the single instance of this class." );
14
14
}
15
15
}
16
16
17
- public static Singleton getInstance (){
17
+ public static SingletonLazyInit getInstance (){
18
18
if (instance == null ){ //if there is no instance available... create new one
19
- synchronized (Singleton .class ){
20
- if (instance == null ) instance = new Singleton ();
19
+ synchronized (SingletonLazyInit .class ){
20
+ if (instance == null ) instance = new SingletonLazyInit ();
21
21
}
22
22
}
23
23
@@ -26,7 +26,7 @@ public static Singleton getInstance(){
26
26
27
27
// TODO - Implement some functionality to pull data or do tasks
28
28
29
- protected Singleton readResolve () {
29
+ protected SingletonLazyInit readResolve () {
30
30
return getInstance ();
31
31
}
32
32
}
You can’t perform that action at this time.
0 commit comments