java设计模式(创建型)之单例模式

第0章:简介

单例模式定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

单例模式本质:控制实例数目。

参考:http://chjavach.iteye.com/blog/721076  ,研磨设计模式(书籍),大话设计模式(书籍)

模式图:

待补充

第1章:实践

第0节:懒汉式单例模式(非线程安全,延迟加载)

package com.mcc.core.designPattern.build.singleton;

/**
 * 懒汉式单例模式,非线程安全,延迟加载
 *
 *
 * @author mailto:417877417@qq.com">menergy>
 *         DateTime: 14-3-8  下午11:13
 */
public class LazySingleton {

    //全局静态实例
    private static LazySingleton instance = null;

    /**
     * 私有构造器控制实例创建数目
     */
    private LazySingleton(){

    }

    /**
     * 全局访问点,非线程安全
     */

    public static LazySingleton getInstance(){
        if(instance == null){
            instance = new LazySingleton();
        }
        return instance;
    }
}
 

第1节:饿汉式单例模式(线程安全,预加载)

package com.mcc.core.designPattern.build.singleton;

/**
 * 饿汉式单例模式,线程安全,类装载的时候就会被加载
 *
 * @author menergy
 *         DateTime: 14-3-8  下午11:31
 */
public class HungrySingleton {
    //只有一个实例
    private static HungrySingleton instance = new HungrySingleton();

    /**
     * 私有构造器控制实例数量
     */
    private HungrySingleton(){

    }

    /**
     * 获取实例
     * @return
     */
    public static HungrySingleton getInstance(){
        return instance;
    }
}

第2节:volatile关键字实现单例模式(线程安全,延迟加载,单实例,推荐使用)

package com.mcc.core.designPattern.build.singleton;

/**
 * volatile关键字实现单例模式,线程安全,延迟加载
 *
 * valatile关键字会屏蔽虚拟机中的一些必要的优化,影响运行效率,非特殊要求一般不用。
 *
 * @author menergy
 *         DateTime: 14-3-8  下午11:40
 */
public class VolatileSingleton {

    //volatile关键字修饰,不会被本地线程缓存,多个线程能正确的处理该变量
    private volatile static VolatileSingleton instance = null;

    /**
     * 私有构造器控制实现数量
     */
    private VolatileSingleton(){

    }

    public static VolatileSingleton getInstance(){
        if (instance == null){
            //同步块,线程安全
            synchronized (VolatileSingleton.class){
                if (instance == null){
                    instance = new VolatileSingleton();
                }
            }
        }
        return instance;
    }

}

第3节:类级内部类实现单例模式(线程安全,延迟加载,推荐使用)

package com.mcc.core.designPattern.build.singleton;

/**
 * 类级内部类单例模式,线程安全,延迟加载,推荐使用
 *
 * @author menergy
 *         DateTime: 14-3-8  下午11:54
 */
public class InnerclassSingleton {

    private static class SingletonHolder{
        //静态初始化,由JVM来保证线程安全
        private static InnerclassSingleton instance = new InnerclassSingleton();
    }

    /**
     * 私有构造器控制实例数量
     */
    private InnerclassSingleton(){

    }

    /**
     * 全局访问点
     * @return
     */
    public static InnerclassSingleton getInstance(){
        return SingletonHolder.instance;
    }
}

第4节:双重检查实现单例模式(线程安全,延迟加载,单实例,推荐使用)

public class SingletonTest06 {

    public static void main(String[] args) {
        System.out.println("双重检查");
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}

// 懒汉式(线程安全,同步方法)
class Singleton {
    //volatile关键字可以将值立即更新到内存
    private static volatile Singleton instance;
    private Singleton() {}
    //提供一个静态的公有方法,加入双重检查代码,解决线程安全问题, 同时解决懒加载问题
    //同时保证了效率, 推荐使用
    public static Singleton getInstance() {
        if(instance == null) {
            synchronized (Singleton.class) {
                if(instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
————————————————
转载如下
原文链接:https://blog.csdn.net/qq_33732195/article/details/104349059

但是:双重检查也不能完全保证单实例,但一般情况下不需要过分考虑,这里只做思想扩展。

参考:Java单例模式中双重检查锁的问题

链接:https://blog.csdn.net/chenchaofuck1/article/details/51702129

第5节:枚举实现单例模式(线程安全,延迟加载,单实例,推荐使用)

/*
*这借助 JDK1.5 中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。
*/
public class SingletonTest08 {

    public static void main(String[] args) {
        Singleton instance = Singleton.INSTANCE;
        Singleton instance2 = Singleton.INSTANCE;
        System.out.println(instance == instance2);
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());
        instance.sayOK();
    }
}

//使用枚举,可以实现单例, 推荐
enum Singleton {
    INSTANCE; //属性
    public void sayOK() {
        System.out.println("ok");
    }
}
————————————————
转载如下
原文链接:https://blog.csdn.net/qq_33732195/article/details/104349059


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值