单例模式
来源:互联网 发布:在淘宝如何绑定银行卡 编辑:程序博客网 时间:2024/06/04 19:35
单例模式
单例模式也是归属于创建型模式
定义
- 实例类确保只有一个实例对象。
- 提供一个全局的访问方法
- 自行实例化
- 构造方法私有
角色
singleton
分类
- 饿汉式
package per.jbz.singleton;/** * 饿汉式 * @author Coding_Jbz * */public class Person { public static final Person person = new Person(); private Person(){ //私有构造 } //提供全局的方法 public static Person getPerson(){ return person; }}
- 懒汉式
/** * 懒汉式 * @author Coding_Jbz * */public class Person2 { public static Person2 person ; private Person2(){ //私有构造 } //提供全局的方法 public static synchronized Person2 getPerson(){ if(null==person){ return person = new Person2(); } return person; }}
- 双向检测
package per.jbz.singleton;/** * 双重检测--懒汉式同步问题造成 * @author Coding_Jbz * */public class Person3 { public static Person3 person ; private Person3(){ //私有构造 } //提供全局的方法 public static Person3 getPerson(){ if(null==person){ synchronized (Person3.class) { if(person==null){ person = new Person3(); } } } return person; }}
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- 12-5和12-6考试总结
- [leecode 170] Two Sum III - Data structure design
- 创建轴网
- JavaScript部分知识点汇总
- 经典排序SQL
- 单例模式
- Unity leapmotion开发实录(3)
- 程序8——二叉树的镜像
- 虚拟机的几种上网方式
- http收藏的文章
- 开发datastage parallel routine
- 第一次使用redis看的文章(转)
- jquery根据label文本查找label元素
- 绘制像素到屏幕