设计模式--单例模式

标题单例模式

好处:保证系统中只有该类的一个实例,防止频繁创建销毁对象。

1:饿汉式:直接在全局进行创建

class Singleton_1 {
   private Singleton_1() {

    }

    // 在装载的时候就进行了类加载,会造成内存浪费
    private static Singleton_1 singleton_1 = new Singleton_1();

    public static Singleton_1 getInstance() {
        return singleton_1;
    }
}

2:饿汉式:使用构造函数创建

// 构造方法中创建
class Singleton_2 {
    // 在装载的时候就进行了类加载,会造成内存浪费
    private static Singleton_2 singleton_2;

    private Singleton_2() {
        singleton_2 = new Singleton_2();
    }


    public static Singleton_2 getInstance() {
        return singleton_2;
    }
}

3:饿汉式:使用静态代码块

class Singleton_3 {
    // 在装载的时候就进行了类加载,会造成内存浪费
    private static Singleton_3 singleton_3;

    Singleton_3() {

    }

    static {
        singleton_3 = new Singleton_3();
    }

    public static Singleton_3 getInstance() {
        return singleton_3;
    }
}

4:懒汉式:判断为空创建

// 懒汉式 ,线程不安全
class Singleton_4 {
   private Singleton_4() {

    }

    // 在装载的时候就进行了类加载,会造成内存浪费
    private static Singleton_4 singleton_4;

    public static Singleton_4 getInstance() {
        if (singleton_4 == null) {
            singleton_4 = new Singleton_4();

        }
        return singleton_4;
    }
}

5:懒汉式:获取实例时加sync


// 懒汉式 ,线程安全, 效率太低, 不推荐
class Singleton_5 {
    Singleton_5() {

    }

    // 在装载的时候就进行了类加载,会造成内存浪费
    public static Singleton_5 singleton_5;

    public static synchronized Singleton_5 getInstance() {
        if (singleton_5 == null) {
            singleton_5 = new Singleton_5();

        }
        return singleton_5;
    }
}

6:懒汉式:创建时加sync

class Singleton_6 {
    Singleton_6() {

    }

    // 在装载的时候就进行了类加载,会造成内存浪费
    private static Singleton_6 singleton_6;

    public static Singleton_6 getInstance() {
        if (singleton_6 == null) {
            synchronized (Singleton_6.class) {
                singleton_6 = new Singleton_6();
            }

        }
        return singleton_6;
    }
}


7:双重检测:获取实例时加sync,创建时加sync

// 推荐
class Singleton_7 {
    Singleton_7() {

    }

    
    private static volatile Singleton_7 singleton_7;

    public static synchronized Singleton_7 getInstance() {
        if (singleton_7 == null) {
            synchronized (Singleton_7.class) {
                if (singleton_7 == null) {
                    singleton_7 = new Singleton_7();
                }
            }

        }
        return singleton_7;
    }
}

8:静态内部类

// 推荐 静态内部类
class Singleton_8 {
    Singleton_8() {
    }

    public static class GetInstance {
        private static final Singleton_8 INSTANCE = new Singleton_8();
    }

    private static  Singleton_8 getInstance() {
        return GetInstance.INSTANCE;
    }
}

9:枚举

// 使用枚举可以实现单例  推荐
enum Singleton {
    INSTANCE;

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

单例模式应用

JDK 中Runtime饿汉式的使用
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值