设计模式-单例模式

设计模式-单例模式

单例模式

单例模式属于创建型模式的一种,应用于保证一个类仅有一个实例的场景下,并且提供了一个访问它的全局访问点,如spring中的全局访问点BeanFactory,spring下所有的bean都是单例。

单例模式的特点:从系统启动到终止,整个过程只会产生一个实例。
单例模式常用写法:懒汉式,饿汉式,注册式,序列化式。

样例代码

懒汉式

package com.demo.test.test2;

/**
 * 单例模式--懒汉式
 */
public class Lazy {

    private static Lazy lazy = null;

    private Lazy(){}

	// 此处加锁有性能问题,见下 双重检查
    public static synchronized Lazy getInstance(){
        if (lazy == null) {
            lazy = new Lazy();
        }
        return lazy;
    }

    public void say(){
        System.out.println("i am lazy");
    }

}

饿汉式

package com.demo.test.test2;

/**
 * 单例模式-饿汉式
 */
public class Hungry {
	// 简单粗暴直接创建,会占用内存. 见 双重检查
    private static final Hungry hungry = new Hungry();

    private Hungry(){}

    public static Hungry getInstance(){
        return hungry;
    }

    public void say(){
        System.out.println("i am hungry");
    }

}

双重检查

package com.demo.test.test2;

/**
 * 单例模式-双重检查
 */
public class LazyCheck {

    // 采用volatile禁止指令重排
    private static volatile LazyCheck lazyCheck = null;

    private LazyCheck() {}

    public static LazyCheck getInstance(){

        if(lazyCheck == null){
            // 减少锁的范围, 提升性能.
            synchronized (LazyCheck.class){
                lazyCheck = new LazyCheck();
            }
        }

        return lazyCheck;
    }


}

静态内部类

package com.demo.test.test2;

/**
 * 单例模式-静态内部类
 */
public class SingletonObject {

    private SingletonObject(){}

    /**
     * 单例持有者
     */
    private static class SingletonObjectHolder{
        private static final SingletonObject singletonObject = new SingletonObject();
    }

    public static SingletonObject getInstance(){
        return SingletonObjectHolder.singletonObject;
    }
}

package com.demo.test.test2;

public class Main {

    public static void main(String[] args) {
        Lazy lazy1 = Lazy.getInstance();
        Lazy lazy2 = Lazy.getInstance();

        System.out.println(lazy1 == lazy2);
        System.out.println(lazy1.equals(lazy2));

        Hungry hungry1 = Hungry.getInstance();
        Hungry hungry2 = Hungry.getInstance();
        System.out.println(hungry1 == hungry2);
        System.out.println(hungry1.equals(hungry2));

        LazyCheck lazyCheck1 = LazyCheck.getInstance();
        LazyCheck lazyCheck2 = LazyCheck.getInstance();
        System.out.println(lazyCheck1 == lazyCheck2);
        System.out.println(lazyCheck1.equals(lazyCheck2));

        SingletonObject singletonObject1 = SingletonObject.getInstance();
        SingletonObject singletonObject2 = SingletonObject.getInstance();
        System.out.println(singletonObject1 == singletonObject2);
        System.out.println(singletonObject1.equals(singletonObject2));
    }
}

执行结果

true
true
true
true
true
true
true
true

参考链接: https://www.cnblogs.com/hyhy904/p/10958554.html.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值