Singleton Pattern ——book review by Mac
来源:互联网 发布:网络销售微信聊天 编辑:程序博客网 时间:2024/06/04 19:00
Ways of singleton pattern can manage the situation where a object can only be created once .They need to trade off to satisfy different ones.
- Single thread singleton
class Singleton{ private Singleton(){}; private static Singleton singleton; public Singleton getInstance(){ if(singleton==null){ singleton=new Singleton(); } return singleton; }}
Multi-thread ones
the less efficient one for multi-thread and lazy created
class Singleton{ private Singleton(){}; private static Singleton singleton; public synchronized Singleton getInstance(){ if(singleton==null){ singleton=new Singleton(); } return singleton; }}
- early created one
public class Singleton{ private static Singletion uniqueInstance=new Singleton(); private Singleton(){}; public Singleton getInstance(){ return uniqueInstance; } }
- double check one
(2)More efficient one
public class Singleton{ private volatile static Singleton uniqueInstance; private Singleton(){}; public static Singleton getInstance(){ if(uniqueInstance==null){ synchronized(Singleton.class){ if(uniqueInstance==null){ uniqueInstance=new Singleton(); } } } }}
Principles
1. synchronized ones costs lots of overhead.
2. double check can not be used in the JVM version of 1.4 or before.
3. In JVM 1.2 or earlier ones the singleton object would be collected by garbage collect system when no reference out of the object points to the object.
4. synchronized method may not be used if not that necessary.
0 0
- Singleton Pattern ——book review by Mac
- Design Pattern—Singleton
- Book Review about Head First Design Pattern
- design pattern——singleton pattern
- design pattern——singleton
- Book Review about Head First Design Pattern 2
- Book Review about Head First Design Pattern 3
- Book Review about Head First Design Pattern 4
- Book Review about Head First Design Pattern 5
- Book Review about Head First Design Pattern 6
- Book Review about Head First Design Pattern 7
- Book Review about Head First Design Pattern 8
- Book Review about Head First Design Pattern 9
- book review
- 单件模式——Singleton Pattern
- 单例模式——Singleton Pattern
- Book Review: ShaderX 7
- javascript设计模式交流(一) ——Singleton Pattern
- 洛神赋
- Hibernate无法自动创建数据库表
- Ubuntu服务器挂载新硬盘的步骤
- Map集合的遍历
- cxf+jms
- Singleton Pattern ——book review by Mac
- 重新开始新的生活了
- The Unordered List Abstract Data Type
- 运用NDWI指数对高分1号进行水体提取--以matlab为例
- Redis异常调查
- 硬盘的分区和分区格式化类型详解
- xcode iOS 模拟器 textField 鼠标点击后键盘不显示
- AOP--代理模式,拦截器的简易实现及原理
- 如何判断两个矩形是否有重叠部分?(某公司校园招聘笔试试题)