Java设计模式小问题
来源:互联网 发布:非农历史数据统计分析 编辑:程序博客网 时间:2024/05/23 22:59
单例模式有几种?如何优化?
单例模式有7种用法
1)public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2)
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
3)
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
4)
public class Singleton {
private Singleton instance = null;
static {
instance = new Singleton();
}
private Singleton (){}
public static Singleton getInstance() {
return this.instance;
}
}
5)
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
6)
public enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}
7)
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
0 0
- Java设计模式小问题
- 关于一些设计模式(面试小问题,Java篇)
- java工厂设计模式小例子
- 设计模式小感
- 设计模式小纪
- 设计模式小总结
- JAVA画板小程序(设计模式课作业)
- Java---设计模式app小软件汇总应用
- 【小话设计模式】设计模式基础
- PCB设计小问题
- 数据库Oracle+java web设计中的小问题
- MVC设计模式小试验
- MVC设计模式---小笔记
- 设计模式课堂小笔记
- java设计模式学习系列之六:Observer, 观察者模式---小例
- [Java] JAVA设计模式
- 设计模式之访问者模式小论
- 小话设计模式一:适配器模式
- java包生成API遇乱码问题解决方法
- 客户端生猛上手之控制台操作
- Webservice之schema
- atomic和unatomic区别
- 自己写的yaf的mysql pdo扩展
- Java设计模式小问题
- codeforces 638B - Making Genome in Berland (STL&&DFS)
- Java里的下载Excel模板上传Excel文件
- selenium运行火狐报错FirefoxDriver : Unable to connect to host 127.0.0.1 on port 7055
- CodeForces 439B Devu, the Dumb Guy
- 23中设计模式
- JFusionChartFree
- 线性表的链式表示和实现----实用算法
- POJ 3070 矩阵快速幂