D:\NewZebra\JavaApplications\DesignPatternsProject\src\creational_patterns\my_singleton\MySingleton.java |
1
2
3
4
5
6 package creational_patterns.my_singleton;
7
8
9
10 @author
11
12 import java.lang.*;
13 import java.util.*;
14 import java.io.*;
15
16
17 public class MySingleton {
18
19 private static MySingleton instance;
20
21
22 private MySingleton() {
23 }
24
25
26
27
28 private synchronized static void createAnInstance(){
29 if(null == instance){
30 instance = new MySingleton();
31 System.out.println("A new MySingleton is created");
32 }
33 }
34
35
36
37 public static MySingleton makeInstance(){
38 if(null == instance)
39 createAnInstance();
40 else
41 System.out.println("A copy of MySingleton is already created");
42 return instance;
43 }
44
45
46
47 protected void finalize() {
48 instance = null;
49 try {
50 super.finalize();
51 }
52 catch(Throwable myThrowable) {
53 System.out.println("finalize() catch clause");
54 }
55 }
56 }
57
58