设计模式(六)——单例模式
来源:互联网 发布:淘宝店铺如何发布微淘 编辑:程序博客网 时间:2024/04/30 23:07
单例模式提供了一种方式使得对象唯一化,一个类只能创建唯一一个实例。单例模式只包含一个Singleton(单例类),在单例类内部实现只生成一个实例,通过一个私有的构造函数,确保用户无法通过new关键字直接实例化单例类,而是通过一个静态的工厂方法来检测实例是否存在并实例化自己,然后储存在静态成员变量中,以确保只有一个实例被创建。
//一般结构public class Singleton { private static Singleton instance = null; private Singleton(){ } public static Singleton getInstance(){ if(instance == null){ instance = new Singleton(); } return instance; }}
具体实例public class IdentityCardNo { private static IdentityCardNo instance = null; private String no; private IdentityCardNo(){ } public static synchronized IdentityCardNo getInstance(){ if(instance == null){ System.out.println("第一次办理身份证,分配新号码"); instance = new IdentityCardNo(); instance.setIdentityCardNo("No123456789"); } else { System.out.println("重复办理"); } return instance; } private void setIdentityCardNo(String no){ this.no = no; } public String getIdentityCardNo(){ return this.no; }}
单例模式的优点:
(1)提供了对唯一实例的访问控制,因为单例类封装了它唯一的实例,所以它可以严格控制客户怎样访问以及何时访问它。
(2)允许可变数目的实例。基于单例模式我们可以进行扩展,使用与单例模式相似的方法来获得指定数目的对象实例。
单例模式的缺点:
(1)单例模式没有抽象层,因此单例类扩展很难。
(2)单例类违背了“单一职责原则”。因为单例类既充当了工厂角色,又充当了产品角色。
(3)滥用单例模式会造成一些负面问题,为了节省资源将数据库连接池设计成单例类可能导致共享连接池对象的程序过多而出现连接池溢出。
(4)垃圾回收技术可能将实例化对象回收,下次利用时又重新实例化,这样造成对象状态的丢失。
适用环境:
(1)系统只需要一个实例对象。
(2)客户调用类的单个实例只允许使用一个公共访问点,除了这个访问点其他途径不能访问实例。
最好使用
饿汉式单例模式
//线程安全,不用加锁都安全,多线程可以直接用public class EagerSingleton { private static final EagerSingleton instance = new EagerSingleton(); private EagerSingleton(){} public static EagerSingleton getInstance(){ return instance; }}
饱汉单例模式
//实现方法一public class LazySingleton { private static LazySingleton instance = null; private LazySingleton(){} synchronized public static LazySingleton getInstance(){ if(instance == null){ instance = new LazySingleton(); } return instance; }}//实现方法二public class LazySingleton { private static LazySingleton instance = null; private LazySingleton(){} public static LazySingleton getInstance() { if (instance == null) { synchronized (LazySingleton.class) { if (instance == null) { instance = new LazySingleton(); } } } return instance; } }//实现方法三public class LazySingleton { private static class LazySingletonCreat{ private static final LazySingleton INSTANCE = new LazySingleton(); } private static LazySingleton INSTANCE = null; private LazySingleton(){} public static LazySingleton getInstance() { return LazySingletonCreat.INSTANCE; } }
0 0
- 设计模式(六)——单例模式
- AspectJ实现设计模式(六)—单例模式
- AspectJ实现设计模式(六)—单例模式
- 探索设计模式之六——单例模式
- 探索设计模式之六——单例模式
- 设计模式六 单例模式
- 设计模式 (六)单例模式(Singleton)
- 设计模式 (六)单例模式(Singleton)
- 设计模式 (六)单例模式(Singleton)
- 设计模式—单例设计模式
- 设计模式—原型模式(六)
- 设计模式(一)——单例设计模式
- 多线程六(单例设计模式)
- 设计模式——单例模式
- 设计模式——单例模式
- 设计模式——单例模式
- 设计模式——单例模式
- 设计模式——单例模式
- 公司的app在魅族4 flyme5.1上打开就黑屏
- 《程序员的自我修养》-编译和链接
- toolbar伸缩折叠
- 蓝桥杯题——BEGIN-2 序列求和 BEGIN-3 圆的面积 BEGIN-4 Fibonacci数列 C语言+Java
- Intent传递Edittext内容到另一个Activity中Textview显示
- 设计模式(六)——单例模式
- android新手,求大神的指点啊
- nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
- Android 退出app再次进入无法获取存放在shareprefenrence中的内容
- 左递归
- JavaAPI详解系列(5):String类(5)
- Sqlite 只能插入1000行
- 安卓开发,Dialog中如何引用Activity中的组件?
- 来电接听问题