系列文章目录
创建型模式 - 单例模式(一)
创建型模式 - 工厂模式(二)
创建型模式 - 原型模式(三)
创建型模式 - 建造者模式(四)
结构型模式 - 适配器模式(一)
结构型模式 - 桥接模式(二)
结构型模式 - 装饰器模式(三)
结构型模式 - 组合模式(四)
结构型模式 - 外观模式(五)
结构型模式 - 享元模式(六)
结构型模式 - 代理模式(七)
行为型模式 - 模板方法模式(一)
行为型模式 - 命令模式(二)
行为型模式 - 访问者模式(三)
行为型模式 - 迭代器模式(四)
行为型模式 - 观察者模式(五)
行为型模式 - 中介者模式(六)
行为型模式 - 备忘录模式(七)
行为型模式 - 解释器模式(八)
行为型模式 - 状态模式(九)
行为型模式 - 策略模式(十)
行为型模式 - 责任链模式(十一)
文章目录
一、 单例模式介绍
- 所谓类的单例设计模式,就是
采用一定的方法保证在整个的软件系统中,对某一个类只能存在一个对象实例,并且该类值提供一个取得其对象实例的方法(静态方法)
;
二、单例模式的创建方式
- 饿汉式(静态常量);
- 饿汉式(静态代码块);
- 懒汉式(线程不安全);
- 懒汉式(线程安全,同步方法);
- 懒汉式(线程不安全,同步代码块);
- 双重检查
- 静态内部类
- 枚举
2.1 饿汉式(静态常量)
步骤:
- 构造器私有化
- 类内部创建对象;
- 向外暴露一个静态的公共方法;
public class Singleton {
public static void main(String[] args) {
Singleton1 singleton1 = Singleton1.getInstance();
Singleton1 singleton2 = Singleton1.getInstance();
System.out.println(singleton1 == singleton2);
System.out.println(String.format("Singleton1 HashCode = %s", singleton1.hashCode()));
System.out.println(String.format("Singleton2 HashCode = %s", singleton2.hashCode()));
// true
// Singleton1 HashCode = 356573597
// Singleton2 HashCode = 356573597
}
}
class Singleton1{
/**
* 私有化构造器
*/
private Singleton1(){}
/**
* 内部创建对象
*/
private static Singleton1 singleton1 = new Singleton1();
public static Singleton1 getInstance() {
return singleton1;
}
}
优缺点说明:
- 这种写法简单,就是在类装载的时候就完成实例化,避免了线程同步问题;
- 在类装载的时候就完成实例化,没有达到懒加载的效果,如果自始至终从未使用过这个实例,则会造成内存浪费;
- 这种方法基于classLoader机制避免了多线程同步问题,不过,instance在类的装载时就进行实例化,在单例模式中大多数都是调用getInstance方法,但是导致类装载的原因有很多种,因此不能确定有其他方法或其他静态方法导致类加载,这时候初始化 instance 就没有达到懒加载的效果;
结论:
- 这种单例模式可用,可能造成内存浪费;
2.2 饿汉式(静态代码块)
public class Singleton {
public static void main(String[] args) {
Singleton2 singleton3 = Singleton2.getInstance();
Singleton2 singleton4 = Singleton2.getInstance();
System.out.println(singleton3 == singleton4);
System.out.println(String.format("singleton3 HashCode = %s", singleton3.hashCode()));
System.out.println(String.format("singleton4 HashCode = %s", singleton4.hashCode()));
// true
// singleton3 HashCode = 356573597
// singleton4 HashCode = 356573597
}
}
class Singleton2{
/**
* 私有化构造器
*/
private Singleton2(){}
/**
* 内部创建对象
*/
private static Singleton2 singleton2 ;
/**
* 静态代码块中创建单例对象
*/
static{
singleton2 = new Singleton2();
}
/**
* 提供一个公共的静态方法,返回实例对象
*/
public static Singleton2 getInstance() {
return singleton2;
}
}
优缺点说明:
- 这种方法和上面的方式其实类似,只不过将类的实例化的过程放在静态代码块中,也是在类装载的时候,就执行了静态代码块中的代码,初始化类的实例,优缺点和上面一样;
结论:
- 这种单例模式可用,但是可能会造成内存浪费;
2.3 懒汉式(线程不安全)
public class Singleton {
public static void main(String[] args) {
Singleton3 singleton5 = Singleton3.getInstance();
Singleton3 singleton6 = Singleton3.getInstance();
System.out.println(singleton5 == singleton6);
System.out.println(String.format("singleton5 HashCode = %s", singleton5.hashCode()));
System.out.println(String.format("singleton6 HashCode = %s", singleton6.hashCode()));
// true
// singleton5 HashCode = 356573597
// singleton6 HashCode = 356573597
}
}
class Singleton3{
private Singleton3(){}
private static Singleton3 singleton3;
public static Singleton3 getInstance(){
if(singleton3 == null){
singleton3 = new Singleton3();
}
return singleton3;
}
}
多线程下调用:
优缺点说明:
- 起到懒加载的效果,但是只能在单线程下使用;
- 在多线程下,一个线程进入了if (instance == null )判断语句块,还没有来得及执行,另外一个线程也通过了这个判断语句,这时就会产生多个实例所以在多线程环境下不可以是用这种方式;
结论:
- 在实际开发中,不要使用这种方式;
2.4 懒汉式(线程安全,同步方法)
public class Singleton {
public static void main(String[] args) {
Singleton4 singleton7 = Singleton4.getInstance();
Singleton4 singleton8 = Singleton4.getInstance();
System.out.println(singleton7 == singleton8);
System.out.println(String.format("singleton7 HashCode = %s", singleton7.hashCode()));
System.out.println(String.format("singleton8 HashCode = %s", singleton8.hashCode()));
// true
// singleton7 HashCode = 1846274136
// singleton8 HashCode = 1846274136
}
}
/**
* 懒汉式(线程安全,同步方法)
*/
class Singleton4{
private Singleton4(){}
private static Singleton4 singleton4;
public static synchronized Singleton4 getInstance(){
if(singleton4 == null){
singleton4 = new Singleton4();
}
return singleton4;
}
}
优缺点:
- 解决了线程安全的问题
- 效率不高,每个线程在想获得类的实例的时候,执行 getInstance() 方法都要同步。而其实这个方法只执行一次实例化代码就够了,后面的想获得该实例,直接 return 就行了。方法进行同步效率太低;
结论:
- 实际开发中,不推荐使用这种方式;
2.5 懒汉式(线程不安全,同步代码块)
public class Singleton {
public static void main(String[] args) {
Singleton5 singleton9 = Singleton5.getInstance();
Singleton5 singleton10 = Singleton5.getInstance();
System.out.println(singleton9 == singleton10);
System.out.println(String.format("singleton9 HashCode = %s", singleton9.hashCode()));
System.out.println(String.format("singleton10 HashCode = %s", singleton10.hashCode()));
// true
// singleton9 HashCode = 356573597
// singleton10 HashCode = 356573597
}
}
/**
* 懒汉式(线程不安全,同步代码块)
*/
class Singleton5{
private Singleton5(){}
private static Singleton5 singleton5;
public static Singleton5 getInstance(){
if(singleton5 == null){
synchronized (Singleton5.class){
singleton5 = new Singleton5();
}
}
return singleton5;
}
}
多线程下:
优缺点:
- 这种方式,本意是对懒汉式线程安全同步方法的改进,,但是
这种同步并不能起到线程同步的作用
,和单例模式线程不安全懒汉模式一样,假如一个线程进入if (instance == null ) 判断语句块,另一个线程也通过了这个判断语句,这时便会产生多个实例;结论:
- 在实际开发中,不能试用这种方式;
2.6 双重检查
public class Singleton {
public static void main(String[] args) {
Singleton6 singleton11 = Singleton6.getInstance();
Singleton6 singleton12 = Singleton6.getInstance();
System.out.println(singleton11 == singleton12);
System.out.println(String.format("singleton11 HashCode = %s", singleton11.hashCode()));
System.out.println(String.format("singleton12 HashCode = %s", singleton12.hashCode()));
// true
// singleton11 HashCode = 356573597
// singleton12 HashCode = 356573597
}
}
/**
* 双重检查
*/
class Singleton6{
private Singleton6(){}
private static volatile Singleton6 singleton6;
public static Singleton6 getInstance(){
if( singleton6 == null ){
synchronized (Singleton6.class){
if( singleton6 == null ){
singleton6 = new Singleton6();
}
}
}
return singleton6;
}
}
优缺点:
- 双重检查是多线程开发中常用到的,我们进行了两次 if 判断块检查,这样可以保证线程安全(必选加上volatile 内存可见性和重排序,不然还会出现问题)
- 实例化代码只用执行一次,后面再次访问时,直接返回实例化对象,也避免反复进行方法的同步;
- 线程安全,延迟加载,效率高;
结论:
- 在实际开发中,推荐使用这种单例设计模式;
2.7 静态内部类
public class Singleton {
public static void main(String[] args) {
Singleton7 singleton13 = Singleton7.getInstance();
Singleton7 singleton14 = Singleton7.getInstance();
System.out.println(singleton13 == singleton14);
System.out.println(String.format("singleton13 HashCode = %s", singleton13.hashCode()));
System.out.println(String.format("singleton14 HashCode = %s", singleton14.hashCode()));
// true
// singleton13 HashCode = 356573597
// singleton14 HashCode = 356573597
}
}
/**
* 静态内部类
*/
class Singleton7{
private Singleton7(){}
private static class SingletonInstance {
private static final Singleton7 INSTANCE = new Singleton7();
}
public static synchronized Singleton7 getInstance(){
return SingletonInstance.INSTANCE;
}
}
优缺点:
- 这种方式采用了类装载的机制来保证初始化实例时只有一个线程;
- 静态内部类 SingletonInstance 在类 Singleton7 被装载时候不会立即实例化,而是在需要实例化时,调用 getInstance 方法,才会装载 SingletonInstance 类,从而完成 Singleton7 的实例化;
- 类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的;
- 避免了线程不安全,利于静态内部类的特点实现延迟加载,效率高;
结论:
- 推荐使用;
2.8 枚举
public class Singleton {
public static void main(String[] args) {
Singleton8 singleton15 = Singleton8.INSTANCE;
Singleton8 singleton16 = Singleton8.INSTANCE;
System.out.println(singleton15 == singleton16);
System.out.println(String.format("singleton15 HashCode = %s", singleton15.hashCode()));
System.out.println(String.format("singleton16 HashCode = %s", singleton16.hashCode()));
// true
// singleton15 HashCode = 356573597
// singleton16 HashCode = 356573597
}
}
/**
* 枚举
*/
enum Singleton8{
INSTANCE;
}
优缺点:
- 不仅能避免多线程同步的问题,而且还能防止反序列化重新创建新的对象;
结论:
- 推荐使用
三、JDK源码分析
Runtime 采用饿汉式;
四、 单例模式总结
4.1 单利模式使用场景
- 单例模式的使用场景:
- 需要频繁的进项创建和销毁对象;
- 创建对象时耗时过多或耗费的资源过多(即:重量级对象),但又经常用到的对象,工具类对象;
- 频繁访问数据库或文件的对象(数据源、session 工厂等);
4.2 单例模式优缺点
- 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建和销毁的对象,使用单例模式可以提高系统性能;
- 当想实例化一个单例类时候,必须要记住使用相应的获取对象的方法,而不是使用 new;
五、 参考文献
- https://www.bilibili.com/video/BV1G4411c7N4?p=37&spm_id_from=pageDriver
- https://www.cnblogs.com/maohuidong/p/7843807.html