设计模式之单例模式

  • 饿汉式

package singleton;

/**
 * @author coffee
 * @Date 2021-04-08 17:12
 */
public class EHanShi {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());
    }

}

/**
 * 饿汉式之静态常量
 */
class Singleton{
    private Singleton() {
    }
    private final static Singleton instance = new Singleton();//属性初始化

    public static Singleton getInstance(){
        return instance;
    }
}

/**
 * 饿汉式静态代码块
 */
class Singleton1{
    public Singleton1() {
    }
    private static Singleton1 instance;
    {
        instance = new Singleton1();//静态代码块初始化
    }
    public static Singleton1 getInstance(){
        return instance;
    }
}

  • 懒汉式

package singleton;

/**
 * @author coffee
 * @Date 2021-04-08 17:22
 */
public class LanHanShi {
    public static void main(String[] args) {
        System.out.println(SingletonOfLazy.getInstance().hashCode());
        System.out.println(SingletonOfLazy.getInstance().hashCode());
    }
}

/**
 * 懒汉式之双重检查线程安全
 * 推荐使用
 */
class SingletonOfLazy2{
    private static volatile SingletonOfLazy2 instance;
    private SingletonOfLazy2(){}


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

/**
 * 懒汉式之线程不安全
 * 实际开发中不使用
 */
class SingletonOfLazy{
    private static SingletonOfLazy instance;
    private SingletonOfLazy(){}
    public static SingletonOfLazy getInstance(){
        if (instance == null){
            instance = new SingletonOfLazy();
        }
        return instance;
    }
}

/**
 * 懒汉式之线程安全
 * 效率低,不推荐使用
 */
class SingletonOfLazy1{
    private static SingletonOfLazy1 instance;
    private SingletonOfLazy1(){}
    public static synchronized SingletonOfLazy1 getInstance(){
        if (instance == null){
            instance = new SingletonOfLazy1();
        }
        return instance;
    }
}

  • 静态内部类

package singleton;

/**
 * 静态内部类实现单例模式
 * 推荐使用
 * @author coffee
 * @Date 2021-04-08 17:38
 */
public class StaticInternalClass {
    private static volatile StaticInternalClass instance;
    private StaticInternalClass(){}

    private static class SingletonInstance{
        private static final StaticInternalClass INSTANCE = new StaticInternalClass();
    }
    public static synchronized StaticInternalClass getInstance(){
        return SingletonInstance.INSTANCE;
    }
}

  • 枚举类

package singleton;

/**
 * 枚举类实现单例模式
 * 推荐使用
 * @author coffee
 * @Date 2021-04-08 17:44
 */
public enum Enum {

    INSTANCE;

    public void sayOK(){
        System.out.println("ok");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值