-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS04_Singleton.java
More file actions
60 lines (55 loc) · 1.14 KB
/
DS04_Singleton.java
File metadata and controls
60 lines (55 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.HashMap;
class Singleton
{
//用来存放对应关系
private static HashMap sinRegistry = new HashMap();
static private Singleton s = new Singleton();
//受保护的构造函数
protected Singleton()
{}
public static Singleton getInstance(String name)
{
if(name == null)
name = "Singleton";
if(sinRegistry.get(name)==null)
{
try{
sinRegistry.put(name , Class.forName(name).newInstance());
}catch(Exception e)
{
e.printStackTrace();
}
}
return (Singleton)(sinRegistry.get(name));
}
public void test()
{
System.out.println("getclasssuccess!");
}
}
class SingletonChild1 extends Singleton
{
public SingletonChild1(){}
static public SingletonChild1 getInstance()
{
return (SingletonChild1)Singleton.getInstance("SingletonChild1");
}
public void test()
{
System.out.println("getclasssuccess111!");
}
}
class SingletonChild2 extends Singleton
{
public SingletonChild1(){}
static public SingletonChild1 getInstance()
{
return (SingletonChild1)Singleton.getInstance("SingletonChild2");
}
public void test()
{
System.out.println("getclasssuccess222!");
}
}
public class DS04_Singleton{
}