Singleton
来源:互联网 发布:linux net ratelimit 编辑:程序博客网 时间:2024/05/01 06:05
Common uses
- The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation.
- Facade objects are often Singletons because only one Facade object is required.
- State objects are often Singletons.
- Singletons are often preferred to global variables because:
- They don't pollute the global namespace (or, in languages withnamespaces, their containing namespace) with unnecessary variables.
- They permit lazy allocation and initialization, where global variables in many languages will always consume resources.
Class diagram
JAVA EXAMPLE:
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
// Private constructor prevents instantiation from other classes
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
private static final Singleton INSTANCE = new Singleton();
// Private constructor prevents instantiation from other classes
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
- Singleton
- Singleton
- singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Chrome3.0,Firefox3.5,Safari 4.0和Opera 10内存占用比较
- 甲骨文言而无信 正设法出售Sun硬件业务
- linux 内存寻址总结
- VS2008SP1下jQuery使用初体验
- ubuntu9.04安装在移动硬盘遇到的问题
- Singleton
- 面向对象程序设计(C++)教学资源[2009]
- 还在路上
- jxl操作Excel输出流
- 你期望企业为你加薪吗?
- 16个经典面试问题回答思路 (转)
- tar命令参数
- dom属性和方法
- linux使用中碰到的问题