单例模式

设计模式分类
设计模式分类
设计模式是针对某类问题的通用解决方案。

单例模式

*静态内部类,枚举,双重检查,饿汉式这几个可以用

懒加载就是用到就创建,用不到就不创建

单例模式

-饿汉式静态常量
饿汉式

public class SingletonTest01 {
    public static void main(String[] args) {

        Singleton instance01 = Singleton.getInstance();
        Singleton instance02 = Singleton.getInstance();
        System.out.println(instance01==instance02);
    }
}
//饿汉式
class Singleton{
    //构造器私有化,外部不能new
    private Singleton() {}
    private final static Singleton instance = new Singleton();
    public static Singleton getInstance() {
        return instance;
    }
}

特点

- 饿汉式静态代码块

public class SingletonTest02 {
    public static void main(String[] args) {

        Singleton instance01 = Singleton.getInstance();
        Singleton instance02 = Singleton.getInstance();
        System.out.println(instance01==instance02);
        System.out.println("instance01's hashcode = "+ instance01.hashCode());
        System.out.println("instance02's hashcode = "+ instance02.hashCode());
    }
}
//静态代码块
class Singleton{
    //构造器私有化,外部不能new
    private Singleton() {}
    private static Singleton instance;
    static{
        instance = new Singleton();
    }
    public static Singleton getInstance() {
        return instance;
    }
}

特点

- 懒汉式(线程不安全)

public class SingletonTest03 {
    public static void main(String[] args) {

        Singleton instance01 = Singleton.getInstance();
        Singleton instance02 = Singleton.getInstance();
        System.out.println(instance01==instance02);
        System.out.println("instance01's hashcode = "+ instance01.hashCode());
        System.out.println("instance02's hashcode = "+ instance02.hashCode());
    }
}

class Singleton{
    //构造器私有化,外部不能new
    private Singleton() {}
    private static Singleton instance;

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

特点
懒汉式(线程安全)
代码:

public class SingletonTest04 {
    public static void main(String[] args) {

        Singleton instance01 = Singleton.getInstance();
        Singleton instance02 = Singleton.getInstance();
        System.out.println(instance01==instance02);
        System.out.println("instance01's hashcode = "+ instance01.hashCode());
        System.out.println("instance02's hashcode = "+ instance02.hashCode());
    }
}

class Singleton{
    //构造器私有化,外部不能new
    private Singleton() {}
    private static Singleton instance;
    //提供一个静态公有方法,使用时才创建instance
    //这就叫懒汉式
    //同时添加了一个关键字SYNCHRONIZED保证线程安全
    //但是这样效率很低,每次都get方法效率很低
    public static synchronized Singleton getInstance() {
        if(instance == null){
        
            instance = new Singleton();
        }
        return instance;
    }
}

特点
懒汉式(线程安全,不能使用)
代码

特点
懒汉式(线程安全,推荐方法)
这里用两个if的原因是这样的,如果程序刚开始运行,有可能两个线程同时都通过了第一个if语句,这个时候先进入同步块的当然可以创建一个新的对象,但是另一个线程还在等待,轮到ta的时候ta还会创建一个新的对象,这就不叫单例模式了,所以里面还得加层if判断。
代码:

public class SingletonTest06 {
    public static void main(String[] args) {

        Singleton instance01 = Singleton.getInstance();
        Singleton instance02 = Singleton.getInstance();
        System.out.println(instance01==instance02);
        System.out.println("instance01's hashcode = "+ instance01.hashCode());
        System.out.println("instance02's hashcode = "+ instance02.hashCode());
    }
}

class Singleton{
    //构造器私有化,外部不能new
    private Singleton() {}
    private static volatile Singleton instance;
    //提供一个静态公有方法,使用时才创建instance
    //这就叫懒汉式
    //这里用了两个同步,一个用volatile同步变量,一个同步代码块
    public static Singleton getInstance() {
        if(instance == null){
            synchronized (Singleton.class) {
                if(instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

特点
在这里插入图片描述
静态内部类
代码:

public class SingletonTest07 {
    public static void main(String[] args) {

        Singleton instance01 = Singleton.getInstance();
        Singleton instance02 = Singleton.getInstance();
        System.out.println(instance01==instance02);
        System.out.println("instance01's hashcode = "+ instance01.hashCode());
        System.out.println("instance02's hashcode = "+ instance02.hashCode());
    }
}

class Singleton{
    //构造器私有化,外部不能new
    private Singleton() {}

    //静态内部类
    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();
    }
    //静态公有方法
    public static synchronized Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }
}

特点:
特点
枚举(推荐使用):
代码:

public class SingletonTest08 {
    public static void main(String[] args) {
        Singleton instance01 = Singleton.INSTANCE;
        Singleton instance02 = Singleton.INSTANCE;
        System.out.println(instance01 == instance02);
        System.out.println("instance01's hashcode = " + instance01.hashCode());
        System.out.println("instance02's hashcode = " + instance02.hashCode());
        instance01.sayOK();
    }
}

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

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

}

特点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值