设计模式系列| 单例模式

892 阅读3分钟

大家好,我是狼王,一个爱打球的程序员

这是设计模式的第六篇,这篇让我们来认识一下单例模式


1、概述

单例模式是一种创建型设计模式, 让你能够保证一个类只有一个实例, 并提供一个访问该实例的全局节点。

单例模式解决了两个问题:

1)保证一个类只有一个实例

2)为该实例提供一个全局访问节点。

2、适用场景

1)如果程序中的某个类对于所有客户端只有一个可用的实例, 可以使用单例模式。 2)如果你需要更加严格地控制全局变量, 可以使用单例模式。

3、实例

单例的实现方式

1)饿汉模式

线程安全,类加载完成就完成了实例化,简单实用,推荐。

public class Singleton {

    private final static Singleton instance = new Singleton();

    public static Singleton getInstance() {
        return instance;
    }

    private Singleton() {
    }
}

2)懒汉模式(线程不安全)

使用时进行实例化,但是线程不安全。

public class Singleton {

    private static Singleton instance;

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    private Singleton() {
    }
}

3)懒汉模式(线程安全)

实用时进行加载,通过Synchronized实现线程安全。多线程情况下,造成线程大量的堵塞,效率降低。

public class Singleton {

    private static Singleton instance;

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    private Singleton() {
    }
}

4)懒汉模式(DCL)

针对上面效率低的情况,减少同步代码块,但是又存在线程不安全问题,多线程情况会出现多个实例。

public class Singleton {

    private static Singleton instance;

    public static Singleton getInstance() {
       if (instance == null) {
              synchronized (Singleton.class) {
                  instance = new Singleton();
              }
        }
        return instance;
    }

    private Singleton() {
    }
}

引入双重检查锁DCL,注意这里要实用volatile关键字,防止指令重排序造的问题,在bean实例化时存在半初始化状态,指令重排序会导致使用半初始化对象。

public class Singleton {

    /**
     * volatile 解决指令重排序导致的问题
     */
    private static volatile Singleton instance;

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
    private Singleton() {
    }
}

5)静态内部类

JVM保证单例,因为JVM加载时class只会被加载一次。内部类不会被加载,在使用时才会被加载。

/**
 * 静态内部类
 * 内部静态类不会自动初始化,只有调用静态内部类的方法,静态域,或者构造方法的时候才会加载静态内部类
 */
public class Singleton {

    private static class SingletonHolder {
        private static Singleton instance = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.instance;
    }

    private Singleton() {
    }
}

6)枚举

实现单例模式三个主要特点:

1、构造方法私有化;

2、实例化的变量引用私有化;

3、获取实例的方法共有。但是枚举方式除外。

使用枚举的好处:

1、避免反射攻击。

2、避免反序列化。

public enum Singleton {

    INSTANCE;
    
    public void doSomeThing(){
        System.out.println("this is singleton by enum");
    }
}

4、总结

优点:

1)一个类只有一个实例。

2)指向该实例的全局访问节点。

3)只会初始化一次(首次访问或或者项目启动即创建)。

缺点:

1)违反单一职责,因为既保证一个类只实例化一次,同时还提供全局访问节点。

2)多线程情况下要单独处理,防止创建多个单例对象。

VX搜:狼王编程 白嫖大量学习资源啦!