Skip to content

Commit 23d2273

Browse files
committed
更新 单例模式
1 parent 0a10a38 commit 23d2273

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.singleton;
25+
26+
/**
27+
* “饿汉式” 的单例实现方式
28+
* <p>
29+
* 可以保证线程安全
30+
*/
31+
public class Director {
32+
33+
/**
34+
* 静态的本类实例
35+
*/
36+
private static final Director INSTANCE = new Director();
37+
38+
/**
39+
* 私有化的构造方法保证不被其它类调用
40+
*/
41+
private Director() {
42+
}
43+
44+
/**
45+
* 客户端调用获取单例实例
46+
*
47+
* @return 单例实例
48+
*/
49+
public Director getInstance() {
50+
return INSTANCE;
51+
}
52+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.singleton;
25+
26+
/**
27+
* “懒汉式” 的单例模式
28+
* <p>
29+
* 内部类在被引用之前不会被类加载器加载,直到客户端调用的时候才被加载
30+
* <p>
31+
* 这个方案是线程安全的
32+
*/
33+
public class LazyInitializationDirector {
34+
35+
/**
36+
* 私有构造方法
37+
*/
38+
private LazyInitializationDirector() {
39+
}
40+
41+
/**
42+
* @return 单例实例
43+
*/
44+
public LazyInitializationDirector getInstance() {
45+
return InstanceHolder.INSTANCE;
46+
}
47+
48+
/**
49+
* 延迟加载,生成 {@link LazyInitializationDirector} 的实例
50+
*/
51+
private static class InstanceHolder {
52+
53+
private static final LazyInitializationDirector INSTANCE = new LazyInitializationDirector();
54+
}
55+
}

0 commit comments

Comments
 (0)