java设计模式—单例设计模式(Singleton Pattern)

本文深入探讨了单例设计模式的实现方式,包括饿汉式、懒汉式及推荐的第三种方式。饿汉式简单直接,但在多线程环境下性能更优;懒汉式考虑了延迟加载,但需解决线程安全问题;第三种方式结合两者优点,无需同步技术,被视为Java中实现单例的标准方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单例设计模式


饿汉式、懒汉式(加锁,影响性能)、第三种方式都可以解决线程安全问题


Concept
A particular class should have only one instance. We will use only that instance whenever we are in need


饿汉式:

public class Singleton {

    //必须是private修饰,不然其它类可以通过Singleton.singleton来获取Singleton实例
    private static Singleton singleton =  new Singleton();

    //必须是private修饰,不然其它类可以通过new Singleton()来获取Singleton实例
    private Singleton(){

    }

    //必须是public,用于其它类获取Singleton实例
    public static Singleton getInstance(){
        return singleton;
    }
}

饿汉式在多线程情况下不会创建多个Singleton实例

懒汉式:

public class Singleton {

    //必须是private修饰,不然其它类可以通过Singleton.singleton来获取Singleton实例
    private static Singleton singleton = null;

    //必须是private修饰,不然其它类可以通过new Singleton()来获取Singleton实例
    private Singleton(){

    }

    //必须是public,用于其它类获取Singleton实例
    public static Singleton getInstance(){
        if(singleton==null){
            singleton = new Singleton();
        }
        return singleton;
    }
}

懒汉式有个问题,就是多线程情况下,会同时创建多个Singleton实例,所有多线程情况下必须加

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

对上面的代码可以优化,执行速度更快

 public static Singleton getInstance(){
        if (singleton == null) {     //加个判断,这样不用所有的线程都进入同步代码块
            synchronized (Singleton.class) {
                if(singleton==null){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }

测试:
@Test
    public void test(){
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1==s2);
    }

结果: true


**两种方式因为构造方法被private修饰,所以不能被继承**

第三种方式(推荐)
This method does not need to use the synchronization technique and eager initialization. It is regarded as the standard method to implement singletons in Java.

public class Singleton {

    private static Singleton singleton = null;

    private Singleton(){ }

    /* 此处使用一个内部类来维护单例 */
    private static class SingletonFactory {
        private static Singleton instance = new Singleton();
    }

    public static Singleton getInstance(){
        return SingletonFactory.instance;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值