设计模式之单例模式
来源:互联网 发布:6s4g网络突然上不了网 编辑:程序博客网 时间:2024/06/05 14:42
单例模式其实很简单,就是将构造函数私有化,然后通过自定义一个方法来获得类的实例。
单利模式分为饿汉模式、懒汉模式
饿汉模式SingletonHungry
public class SingletonHungry { private SingletonHungry(){ } private static SingletonHungry instance = new SingletonHungry(); public static SingletonHungry getInstance(){ return instance; }}
懒汉模式SingletonFull
public class SingletonFull { private SingletonFull(){ } private static SingletonFull instance; public static SingletonFull getInstance(){ if(instance == null){ instance = new SingletonFull(); } return instance; }}
测试类
public class Test { public static void main(String[] args){ /* //饿汉模式 SingletonHungry s1 = SingletonHungry.getInstance(); SingletonHungry s2 = SingletonHungry.getInstance(); if(s1 == s2){ System.out.println("s1和s2是同一个实例"); }*/ //懒汉模式 SingletonFull s1 = SingletonFull.getInstance(); SingletonFull s2 = SingletonFull.getInstance(); if(s1 == s2){ System.out.println("s1和s2是同一个实例"); } }}
控制台输出:s1和s2是同一个实例
饿汉模式与懒汉模式的区别
饿汉模式:在类加载的时候就新建了实例,所以类加载速度比较慢,但程序运行速度比较快,因为是静态的初始实例,所以是线程安全的。
懒汉模式:在需要的时候再新建实例,所以类加载速度比较快,但程序运行速度比较慢(也只是第一次获取实例时)。由于类加载的是并没有new 实例,所以当多线程的时候,可能同时new 多个实例,所以是线程不安全的。
懒汉模式如何实现线程安全?
有三种方法
第一种方法是给获取实例的方法加上同步锁
public class SingletonFull { private SingletonFull(){ } private static SingletonFull instance; public static synchronized SingletonFull getInstance(){ if(instance == null){ instance = new SingletonFull(); } return instance; }}
第二种方法是给new 实例加上同步锁
public class SingletonFull { private SingletonFull(){ } private static SingletonFull instance; public static SingletonFull getInstance(){ synchronized(SingletonFull.class){ if(instance == null){ instance = new SingletonFull(); } } return instance; }}
第三种方法是双重校验锁,结合第一种和第二种方法
public class SingletonFull { private SingletonFull(){ } private static SingletonFull instance; public static synchronized SingletonFull getInstance(){ synchronized(SingletonFull.class){ if(instance == null){ instance = new SingletonFull(); } } return instance; }}
阅读全文
0 0
- 设计模式之 单例设计模式
- 设计模式之 单例设计模式
- 设计模式之单例设计模式
- 设计模式之-----------单例设计模式
- 设计模式之:单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之-单例设计模式
- 设计模式之单例设计模式 标签: 设计模式
- 设计模式之单例
- 设计模式之单例
- 设计模式之 单例
- Tomcat密码爆破小记
- java画矩形
- Java和Groovy性能测试
- 使用Sencha cmd 创建Extjs6.2项目
- jQuery选择器文字攻略
- 设计模式之单例模式
- Spring-获取路径
- c++对C语言的扩展(一)
- python篇1_细枝末节
- CF835D Palindromic characteristics(dp)
- HTML5多媒体素材应用
- 作用域链与原型链的区别与联系
- Ubuntu16.04安装mariadb
- Android开发之如何使用Eclipse调试程序