单例设计模式

        单例模式,是一种常用的软件设计模式。通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例。即一个类只有一个对象实例。

具体的思路是:

(1)别人不能new实例,所以要将构造方法私有化,使其不能在类的外部通过new关键字实例化该类对象。

(2)在该类内部产生一个唯一的实例化对象,并且将其封装为private static类型。

(3)定义一个静态方法返回这个唯一对象。        

一、饿汉式

        立即加载就是使用类的时候已经将对象创建完毕(不管以后会不会使用到该实例化对象,先创建了再说。很着急的样子,故又被称为“饿汉模式”),常见的实现办法就是直接new实例化。

public class Singleton {

    // 初始化一个
    private static final Singleton INSTANCE = new Singleton();


    // 让别人不能new
    private Singleton() {

    }
    
    // 保证别人能获取(getInstance:获取实例)
    public static Singleton getInstance() {
        return INSTANCE;
    }
}
public class Client {
    public static void main(String[] args) {

        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        Singleton instance3 = Singleton.getInstance();
        System.out.println(instance1 == instance2);     // true
        System.out.println(instance2 == instance3);     // true

    }
}

二、懒汉式

        延迟加载就是调用get()方法时实例才被创建(先不急着实例化出对象,等要用的时候才给你创建出来。不着急,故又称为“懒汉模式”),常见的实现方法就是在get方法中进行new实例化。

public class Singleton {

    // 初始化一个
    private static Singleton INSTANCE;


    // 让别人不能new
    private Singleton() {

    }

    // 保证别人能获取(getInstance:获取实例)
    public static Singleton getInstance() {
        if (INSTANCE == null){
            INSTANCE = new Singleton();
        }
        return INSTANCE;
    }
}
public class Client {
    public static void main(String[] args) {

        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        Singleton instance3 = Singleton.getInstance();
        System.out.println(instance1 == instance2);     // true
        System.out.println(instance2 == instance3);     // true

    }
}

这种懒汉式在多线程环境中是完全错误的,根本不能保证单例的状态!!

三、内部类实现单例(懒汉式)

        这种也是懒汉式的一种实现,而且使用这种懒汉式没有任何的线程问题,大家来思考,结合咱们上边的内容,只要不调用getInstance()方法,就不会使用内部类,内部类一旦被使用只会被初始化一次,以后一直用的就是静态常量 INSTANCE 了

public class Singleton {
    
    // 让别人不能new
    private Singleton() {

    }

    // 获取实例
    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }


    // 静态内部类会在第一次使用的时候加载一次,静态常量会在类加载后初始化,一次!
    private static class SingletonHolder {
        private static final Singleton INSTANCE = new Singleton();
    }
}
public class Client {
    public static void main(String[] args) {

        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        Singleton instance3 = Singleton.getInstance();
        System.out.println(instance1 == instance2);     // true
        System.out.println(instance2 == instance3);     // true

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值