美文网首页
设计模式 - 单例模式 (1/23)

设计模式 - 单例模式 (1/23)

作者: 夹板儿孩 | 来源:发表于2022-11-29 21:34 被阅读0次

单例模式

饿汉式 (静态常量)

优点:写法简单,在类装载时就完成了实例化,避免了线程同步问题

缺点:在类装载时就完成了实例化,有可能从始至终都用不上这个实例,造成内存浪费

public class Single {
    private Single() {}

    private final static Single single = new Single();

    public static Single getInstance() {
        return single;
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Single.getInstance() == Single.getInstance());
    }
}

console

true

饿汉式 (静态代码块)

这种方式实际与静态常量方式类似,都是在类加载时实例化。优缺点也与静态常量方式相同

public class Single2 {
    static {
        single = new Single2();
    }

    private Single2() {}

    private final static Single2 single;

    public static Single2 getInstance() {
        return single;
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Single.getInstance() == Single.getInstance());
        System.out.println(Single2.getInstance() == Single2.getInstance());
    }
}

console

true
true

懒汉式

有可能会出现多个线程同时抢到了 getInstance 方法的情况,所以线程不安全,慎用

可以通过添加 synchronized 来添加线程锁使线程同步,但是会造成线程拥堵。导致效率变低

public class Single3 {
    private Single3() {}

    private static Single3 single = null;

    public static Single3 getInstance() {
        if (null == single) single = new Single3();
        return single;
    }
    
//    public static synchronized Single3 getInstance() {
//        if (null == single) single = new Single3();
//        return single;
//    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Single.getInstance() == Single.getInstance());
        System.out.println(Single2.getInstance() == Single2.getInstance());
        System.out.println(Single3.getInstance() == Single3.getInstance());
    }
}

console

true
true
true

懒汉式(二)

通过双重验证,再加上 volatile 关键字对变量的修饰达到懒加载/安全并且速度快的单例结果

volatile 详解

public class Single4 {
    private Single4() {}

//    private static Single4 single1 = null;

    /**
     * 添加 volatile 关键字
     * 1、volatile保证可见性
     * 2、volatile不保证原子性
     * 3、volatile禁止指令重排
     */
    private static volatile Single4 single = null;

    /* 反面教材 */
//    public static Single4 getInstance() {
//        if (null == single1) {
//            // 方法没有加锁,无法阻止多线程进入 if 方法
//            synchronized (Single4.class) {
//                single1 = new Single4();
//            }
//        }
//        return single1;
//    }

    /**
     * 通过双重验证,再加上 volatile 关键字对变量的修饰达到懒加载/安全并且速度快的单例结果
     * @return
     */
    public static Single4 getInstance() {
        if (null == single) {
            synchronized (Single4.class) {
                if (null == single) {
                    single = new Single4();
                }
            }
        }
        return single;
    }
}

console

true
true
true
true
true

静态内部类(推荐使用)

线程安全

通过 jvm 虚拟机装载机制来实现单例模式

  1. 当外部类在被装载时,内部类不会被装载
  2. 当外部类使用到内部类时,才会装载到内部类。jvm 在装载类时线程是安全的。并且静态内部类只会被加载一次
public class Single5 {
    private Single5() {}

    private static class SingleInstance {
        private static final Single5 single = new Single5();
    }

    public static Single5 getInstance() {
        return SingleInstance.single;
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Single.getInstance() == Single.getInstance());
        System.out.println(Single2.getInstance() == Single2.getInstance());
        System.out.println(Single3.getInstance() == Single3.getInstance());
        System.out.println(Single4.getInstance() == Single4.getInstance());
        System.out.println(Single5.getInstance() == Single5.getInstance());
    }
}

console

true
true
true
true
true

枚举 (推荐使用)

通过枚举实现单例还可以防止被反射进行多次实例化。是目前最完美的单例机制。

public enum Single6 {
    INSTANCE;

    public String method() {
        return "Hello World";
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Single.getInstance() == Single.getInstance());
        System.out.println(Single2.getInstance() == Single2.getInstance());
        System.out.println(Single3.getInstance() == Single3.getInstance());
        System.out.println(Single4.getInstance() == Single4.getInstance());
        System.out.println(Single5.getInstance() == Single5.getInstance());
        System.out.println(Single6.INSTANCE == Single6.INSTANCE);
        System.out.println(Single6.INSTANCE.method());
    }
}

console

true
true
true
true
true
true
Hello World

相关文章

  • Go语言设计模式(1)单例模式

    Go语言设计模式(1)单例模式 单例模式的定义 个人认为单例模式是23种设计模式中最简单也最好理解的一种,定义如下...

  • 23设计模式之一

    简述 一、设计模式的六大原则 二、23种设计模式 1.单例模式 懒汉式单例 饿汉单例 2.三种工厂模式 1>.简单...

  • 简单&易懂的线程安全单例模式

    1.前言 单例模式是23种设计模式中最常见的设计模式之一,正确编写单例模式是每个程序员都需要掌握的技术,单例模式也...

  • 设计模式分类

    经典23种设计模式: 创建型设计模式: Singleton Pattern(单例模式) PrototypePatt...

  • 单例模式

    单例 单例模式,是一种设计模式,属于创建型设计模式,还有一种创建型设计模式,工厂模式。设计模式总共有23种,三大类...

  • 23种设计模式之《单例模式》

    个人博客: http://zhangsunyucong.top 什么是单例模式 单例模式是23种设计模式中最简单和...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • 设计模式

    设计模式简介 单例设计模式 问题:单例设计模式是什么?为什么要学它?怎么用它? 1.定义:单例模式(Singlet...

  • 2018-07-05 设计模式(前10种)

    23种设计模式总结 1.单例模式(Singleton Pattern) 定义:Ensure a class has...

  • 设计模式

    23种设计模式; 1.创建型模式5种 1.单例模式、抽象工厂模式、建造者模式、原型模式 2.并发设计模式 代理、组...

网友评论

      本文标题:设计模式 - 单例模式 (1/23)

      本文链接:https://www.haomeiwen.com/subject/gserfdtx.html