设计模式基础篇-01-单例模式

1.饿汉式

1. 1.直接创建对象,不管你需不需要都会创建 简单直观

要点:

  • 1.构造器私有化,因为不能让外面去创建实列
  • 2.自己创建,用静态变量保存
  • 3.向外提供实列
  • 4.为了强调这是一个单列,我们可以用final修饰
public class Singleton1 {

    private static final Singleton1 INSTANCE = new Singleton1();

    private Singleton1() {
    }
}

1. 2. 枚举类型

表示该类对象是有限的几个,我们限定为一个就成了单列了, 枚举类型的构造器默认都是私有化的,且重写了toSting方法

public enum singleton2 {
    INSTANCE;
}

1. 3. 使用静态代码块的方式,是1.1的复杂版

主要用于在给对象动态赋值

public class singleton3 {

    private static final singleton3 INSTANCE;

    private  String info;

    static {
        Properties pro = null;
        try {
             pro = new Properties();
            pro.load(singleton3.class.getClassLoader().getResourceAsStream("sinleton.properties"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        INSTANCE = new singleton3(pro.getProperty("info"));
    }

    private singleton3(String info) {
        this.info = info;
    }


    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "singleton3{" +
                "info='" + info + '\'' +
                '}';
    }

    public static void main(String[] args) {
        System.out.println(singleton3.INSTANCE);
    }
}

2. 懒汉式

2.1.延迟创建对象 存在线程安全问题

要点:

  • 1.构造器私有化,因为不能让外面去创建实列
  • 2.静态变量保存实列
  • 3.延迟创建对象

2.2 延迟创建对象

public class singleton4 {

    private static volatile singleton4 INSTANCE;

    private singleton4() {
    }

    //调用此方法时再创建对象 DCL双端检索机制保证单列模式安全问题
    public static singleton4 getInstance() {
        if (INSTANCE == null) {
            synchronized (singleton4.class) {
                if (INSTANCE == null) {
                    INSTANCE = new singleton4();
                }
                return INSTANCE;
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {

        Callable<singleton4> ca = () -> singleton4.getInstance();

        ExecutorService ex = Executors.newFixedThreadPool(2);
        Future<singleton4> s1 = ex.submit(ca);
        Future<singleton4> s2 = ex.submit(ca);

        try {
            System.out.println(s1.get());
            System.out.println(s2.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        ex.shutdown();
    }

}

2.3 相对使用双端检索机制,我们可以采用 内部静态类来达到线程安全问题

public class singleton5 {

    private singleton5() {
    }

    private static class inner {
        private static final singleton5 INSTANCE = new singleton5();
    }

    //静态内部类不会随着外部类的初始化而初始化,而是在调用此方法的时候才会去初始化
    public static singleton5 getInstance() {
        return inner.INSTANCE;
    }

}

更推荐最后这种

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan0517

感谢您的鼓励与支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值