Skip to content

Commit daeac25

Browse files
committed
Fix Sington Seample
1 parent f196bb2 commit daeac25

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

GOF/gof-example/src/main/java/com/ipipman/gof/example/singleton/IdGenerator3.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,35 @@
99

1010
public class IdGenerator3 {
1111

12-
private static volatile IdGenerator3 idGenerator3 = null;
12+
private static volatile IdGenerator3 instance = null;
1313

14-
private IdGenerator3() {
14+
private int generatorId = 0;
15+
16+
private IdGenerator3(){
1517

1618
}
1719

18-
public static IdGenerator3 getInstance() {
19-
if (idGenerator3 == null) {
20-
synchronized (IdGenerator3.class) {
21-
if (idGenerator3 == null) {
22-
idGenerator3 = new IdGenerator3();
20+
public static IdGenerator3 getInstance(){
21+
if (instance == null){
22+
synchronized (IdGenerator3.class){
23+
if (instance == null){
24+
instance = new IdGenerator3();
2325
}
2426
}
2527
}
26-
return idGenerator3;
28+
return instance;
2729
}
2830

29-
public int getId() {
30-
return 1;
31+
public int getGeneratorId(){
32+
return generatorId++;
3133
}
3234

33-
// TODO Testing...
35+
3436
public static void main(String[] args) {
35-
IdGenerator3 idGenerator3 = IdGenerator3.getInstance();
36-
System.out.println(idGenerator3.getId());
37+
for (int i = 0; i < 10; i++) {
38+
System.out.println(IdGenerator3.getInstance().getGeneratorId());
39+
}
40+
3741
}
42+
3843
}

0 commit comments

Comments
 (0)