Skip to content

Commit a8512dc

Browse files
committed
Added enum singleton for reference sake
1 parent 2d581e3 commit a8512dc

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.philipjhamilton;
22

3-
import com.philipjhamilton.patterns.creational.singleton.Singleton;
3+
import com.philipjhamilton.patterns.creational.singleton.SingletonLazyInit;
44

55
/**
66
* Hello world!
@@ -10,7 +10,7 @@ public class App
1010
{
1111
public static void main( String[] args )
1212
{
13-
Singleton test = Singleton.getInstance();
13+
SingletonLazyInit test = SingletonLazyInit.getInstance();
1414

1515
}
1616
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

src/main/java/com/philipjhamilton/patterns/creational/singleton/Singleton.java renamed to src/main/java/com/philipjhamilton/patterns/creational/singleton/SingletonLazyInit.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
import java.io.Serializable;
44
import java.util.HashMap;
55

6-
public class Singleton implements Serializable {
6+
public class SingletonLazyInit implements Serializable {
77

8-
private static volatile Singleton instance = null;
8+
private static volatile SingletonLazyInit instance = null;
99
private static HashMap<String, String> data = new HashMap<String, String>();
1010

11-
private Singleton(){
11+
private SingletonLazyInit(){
1212
if (instance != null){
1313
throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
1414
}
1515
}
1616

17-
public static Singleton getInstance(){
17+
public static SingletonLazyInit getInstance(){
1818
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();
2121
}
2222
}
2323

@@ -26,7 +26,7 @@ public static Singleton getInstance(){
2626

2727
// TODO - Implement some functionality to pull data or do tasks
2828

29-
protected Singleton readResolve() {
29+
protected SingletonLazyInit readResolve() {
3030
return getInstance();
3131
}
3232
}

0 commit comments

Comments
 (0)