Singleton Pattern
2016-07-23 05:18
357 查看
Singleton Pattern:
1. Early mode
public class President{
private static President instance = new President();
private President() {}
public static President getInstance() {
return instance;
}
}
2. Lazy mode:
public class President{
private static President instance;
private President() {}
public static synchronized President getInstance() {
if(instace == null){
instance = new President();
}
return instance;
}
}
3. Optimize solution: (double checked locking)
public class President{
private static volatile President instance;
private President() {}
public static President getInstance() {
if(instance == null) {
synchronized(President.class){
if(instance == null){
instance = new President();
}
}
}
return instance;
}
}
1. Early mode
public class President{
private static President instance = new President();
private President() {}
public static President getInstance() {
return instance;
}
}
2. Lazy mode:
public class President{
private static President instance;
private President() {}
public static synchronized President getInstance() {
if(instace == null){
instance = new President();
}
return instance;
}
}
3. Optimize solution: (double checked locking)
public class President{
private static volatile President instance;
private President() {}
public static President getInstance() {
if(instance == null) {
synchronized(President.class){
if(instance == null){
instance = new President();
}
}
}
return instance;
}
}
相关文章推荐
- Fast Power
- Generate Parentheses
- 关于在MySQL中实现upsert操作
- 利用无线网卡建立个人wifi hotspot
- codeforces 701C They Are Everywhere(尺取法)
- POJ1328-Radar Installation
- 【CSS】其他CSS属性和特性
- ST表与二分 (CodeForces 689D-Friends and Subsequences)
- jvm 配置,看看
- Reverse Words in a String II
- Git命令小总结
- Unity各个函数的执行顺序
- Intersection of Two Arrays
- Codeforces Round #364 (Div. 2) 题解
- 面试基础
- 利用jar命令打包和解压
- JavaScript创建对象
- 【C++11】C++11新增的便利算法[1] all_of、any_of 和 none_of
- 暴力枚举,dfs(连连看,HDU 4272)
- DroidPlugin源码分析服务与静态广播的处理