File tree 1 file changed +18
-13
lines changed
GOF/gof-example/src/main/java/com/ipipman/gof/example/singleton
1 file changed +18
-13
lines changed Original file line number Diff line number Diff line change 9
9
10
10
public class IdGenerator3 {
11
11
12
- private static volatile IdGenerator3 idGenerator3 = null ;
12
+ private static volatile IdGenerator3 instance = null ;
13
13
14
- private IdGenerator3 () {
14
+ private int generatorId = 0 ;
15
+
16
+ private IdGenerator3 (){
15
17
16
18
}
17
19
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 ();
23
25
}
24
26
}
25
27
}
26
- return idGenerator3 ;
28
+ return instance ;
27
29
}
28
30
29
- public int getId () {
30
- return 1 ;
31
+ public int getGeneratorId () {
32
+ return generatorId ++ ;
31
33
}
32
34
33
- // TODO Testing...
35
+
34
36
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
+
37
41
}
42
+
38
43
}
You can’t perform that action at this time.
0 commit comments