学习记录之单例模式(Singleton)--创建型模式

1. Singleton 介绍

意图:保证整个软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得对象实例的方法。

2. Singleton的八种实现方式

2.1 饿汉式(静态常量)

public class User01 {
    public static void main(String[] args) {
        Singleton is01 = Singleton.getInstance();
        Singleton is02 = Singleton.getInstance();
        System.out.println(is01 == is02); // true
        System.out.println("is01.hashCode(): " + is01.hashCode()); // is01.hashCode(): 460141958
        System.out.println("is02.hashCode(): " + is02.hashCode()); // is02.hashCode(): 460141958

    }
}

class Singleton {
    // 1.构造器私有化, 外部不能通过new 创建对象
    private Singleton() {

    }

    // 2. 本类内部创建对象实例,类加载时将完成实例化
    private final static Singleton instance = new Singleton();

    // 3.提供外部获取实例对象的静态方法
    public static Singleton getInstance() {
        return instance;
    }
}
  • 优点:类加载时完成实例化,避免线程同步问题。
  • 缺点:因为在类加载时就完成了实例化,没有起到懒加载的效果,如果程序从始至终没有使用到这个实例,则会造成内存浪费。
  • 结论:这种单例模式可用,但会造成内存浪费。

2.2 饿汉式(静态代码块)

class Singleton {
    // 1.构造器私有化, 外部不能通过new 创建对象
    private Singleton() {
    }

    private static Singleton instance;

    // 2.静态代码块中,创建对象实例
    static {
        instance = new Singleton();
    }
    
    // 3.提供外部获取实例对象的静态方法
    public static Singleton getInstance() {
        return instance;
    }
}
  • 优缺点和结论与饿汉式(静态常量)相同。

2.3 懒汉式(线程不安全)

class Singleton {
    // 1.构造器私有化, 外部不能通过new 创建对象
    private Singleton() {
    }

    private static Singleton instance;

    // 2.当外部使用到实例时,才去创建instance,即懒汉式
    public static Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}
  • 优点:起到懒加载效果。
  • 缺点:只能在单线程下使用,如果在多线程下,当线程A通过了if (instance == null)判断语句块,还没有向下执行,另一个线程B也通过了这个判断语句时,便会产生多个实例。
  • 结论:在实际开发中,不可使用这种方式。

2.4 懒汉式(线程安全,同步方法)

class Singleton {
    // 1.构造器私有化, 外部不能通过new 创建对象
    private Singleton() {
    }

    private static Singleton instance;

    // 2.使用synchronized关键字加锁
    public static synchronized Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}
  • 优点:解决了线程安全问题。
  • 缺点:将方法加锁之后效率太低。当多个线程同时想要获取实例时都要进行等待以获取锁。
  • 结论:在实际开发中,不推荐使用这种方式。

2.5 懒汉式(线程不安全,同步代码块)

class Singleton {
    // 1.构造器私有化, 外部不能通过new 创建对象
    private Singleton() {
    }

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

这种方式针对上面对方法加锁造成效率较低的问题,将锁加在代码块中缩小锁的范围以求加快效率,但这种方式解决不了线程不安全问题,在实际开发中,不可使用。

2.6 双重检查

class Singleton {
    // 1.构造器私有化, 外部不能通过new 创建对象
    private Singleton() {
    }

    // 2.加入volatile关键字,保证可见性、有序性,但不保证原子性
    private static volatile Singleton instance;

    // 3.加入了双重检查
    public static Singleton getInstance(){
        if (instance == null){
            synchronized(Singleton.class){
                if (instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
  • 优点:进行了两次 if (instance == null)检查,线程安全,延迟加载,效率较高。
  • 结论:在实际开发中,推荐使用这种单例设计模式。

2.7 静态内部类

class Singleton {
    // 1.构造器私有化, 外部不能通过new 创建对象
    private Singleton() {
    }
    
    // 2.静态内部类,该类中有一个静态属性Singleton
    private static class SingletonInstance{
        private final static Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }
}
  • 优点:线程安全,利用静态内部类实现延迟加载,效率高。
  • 这种方式采用类加载的机制来保证初始化实例时只有一个线程。
  • 静态内部类SingletonInstance在Singleton类被加载时并不会立即实例化,而是当外部调用getInstance()方法时,才会装载SingletonInstance类,从而完成Singleton的实例化。
  • 结论:推荐使用。

2.8 枚举

public class User01 {
    public static void main(String[] args) {
        Singleton is01 = Singleton.INSTANCE;
        Singleton is02 = Singleton.INSTANCE;
        System.out.println(is01 == is02); // true
        System.out.println("is01.hashCode(): " + is01.hashCode()); // is01.hashCode(): 460141958
        System.out.println("is02.hashCode(): " + is02.hashCode()); // is02.hashCode(): 460141958
        is01.sayOK();
    }
}

enum  Singleton {
    INSTANCE;
    public void sayOK(){
        System.out.println("OK!");
    }
}
  • 借助枚举实现单例模式,不仅能避免多线程安全问题,而且还能防止反序列化重新创建新的对象。
  • 结论:强烈推荐使用。

应用场景

需要频繁进行创建和销毁的对象, 创建对象时耗时过多或耗费资源过多,但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象(例如:数据源、session工厂等)。
在JDK中,java.lang.Runtime使用到了单例模式中的饿汉式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值