单例模式(Singleton)

一般实现

  • 创建执行方法
public class WithoutSingleton {
    public static void withoutSingletonInfo(WithoutSingleton withoutSingleton){
        System.out.println("WithoutSingleton.hashCode = " + withoutSingleton.hashCode());
    }
}
  • 测试类
    public static void main(String[] args) {
        WithoutSingleton withoutSingleton = new WithoutSingleton();
        WithoutSingleton.withoutSingletonInfo(withoutSingleton);
        WithoutSingleton withoutSingleton2 = new WithoutSingleton();
        WithoutSingleton.withoutSingletonInfo(withoutSingleton2);
        WithoutSingleton withoutSingleton3 = new WithoutSingleton();
        WithoutSingleton.withoutSingletonInfo(withoutSingleton3);
    }
  • 输出
WithoutSingleton.hashCode = 2083479002
WithoutSingleton.hashCode = 163238632
WithoutSingleton.hashCode = 1215070805
  • 问题
    每次生成的类的实例对象都是不同的,但是在一些特定的场合,需要每次返回的实例对象都是同一个,如:sping中创建bean。

懒汉式单例模式

  • 单例类
public class LazySingleton {
    private static LazySingleton lazySingleton = null;
    private LazySingleton(){
    }
    public static LazySingleton getInstance(){
        if(lazySingleton == null){
            lazySingleton = new LazySingleton();
        }
        return lazySingleton;
    }
    public static void singletonInfo(){
        System.out.println("lazySingleton.hashCode = " + lazySingleton.hashCode());
    }
}
测试类:
    public static void main(String[] args) {
        LazySingleton lazySingleton = LazySingleton.getInstance();
        lazySingleton.singletonInfo();
        LazySingleton lazySingleton2 = LazySingleton.getInstance();
        lazySingleton2.singletonInfo();
        LazySingleton lazySingleton3 = LazySingleton.getInstance();
        lazySingleton3.singletonInfo();
    }
输出:
lazySingleton.hashCode = 1110594262
lazySingleton.hashCode = 1110594262
lazySingleton.hashCode = 1110594262
  • 实现方式

    构造方法私有化。

  • 存在问题

    线程不安全,如果多个线程同时访问,仍会产生多个实例对象。

  • 解决方法
    1.在getInstance()方法上同步synchronized(同步对性能有影响)
    2.双重检查锁定
    3.静态内部类

饿汉式单例模式

  • 代码实现
public class HungrySingleton {
    private static HungrySingleton hungrySingleton = new HungrySingleton();
    private HungrySingleton(){
    }
    public static HungrySingleton getInstance(){
        return hungrySingleton;
    }
    public static void singletonInfo(){
        System.out.println("hungrySingleton.hashCode = " + hungrySingleton.hashCode());
    }
}
  • 输出
hungrySingleton.hashCode = 1977385357
hungrySingleton.hashCode = 1977385357
hungrySingleton.hashCode = 1977385357
  • 说明
    静态成员变量在类加载的时候就会创建实例对象,解决了线程不安全问题
  • 缺点
    类加载时就创建实例对象,会占用系统内存,如果存在大量单例类(一般不会出现),或者创建的类并没有使用,会造成内存浪费。

源码

https://github.com/Seasons20/DisignPattern.git

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值