设计模式之单例模式

单例设计模式介绍

类的单例设计模式:就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例。并且该类只提供一个取得对象实例的方法(静态方法)

单例模式特点:

  • 单例类只能由一个实例。
  • 单例类必须自己建立自己的唯一实例。
  • 单例类必须对外提供这个唯一实例。

单例模式的实现

单例模式有7中时间方式:

  • 饿汉式(两种)
  • 懒汉式(三种)
  • 双重检查
  • 静态内部类
  • 枚举

饿汉式

1.饿汉式(静态常量):

class Singleton{
    //防止被实例化
    private Singleton(){

    }
    //创建对象实例
    private static final Singleton instance = new Singleton();

    //提供一个静态方法,返回实例对象
    public static Singleton getInstance(){
        return instance;
    }

}

2.饿汉式(静态代码块)

class Singleton{
    //防止被实例化
    private Singleton(){

    }
    
    private static Singleton instance;
    
    //创建对象实例
    static {
        instance = new Singleton();
    }

    //提供一个静态方法,返回实例对象
    public static Singleton getInstance(){
        return instance;
    }

}

优点:避免线程同步问题。
缺点:在类加载的时候就完成实例化,没有达到懒加载的效果。如果没使用过这个实例,则会造成内存的浪费。

懒汉式

1.懒汉式(线程不安全):

class Singleton{
    //防止被实例化
    private Singleton(){

    }

    private static Singleton instance;
    
    //提供一个静态方法,返回创建返回实例对象
    public static Singleton getInstance(){
       if(instance==null){
           instance = new Singleton();
       }
       return instance;
    }

}

2.懒汉式(线程安全)

class Singleton{
    //防止被实例化
    private Singleton(){

    }

    private static Singleton instance;

    //提供一个静态方法,返回创建返回实例对象
    public static synchronized Singleton getInstance(){
       if(instance==null){
           instance = new Singleton();
       }
       return instance;
    }

}

3.懒汉式(线程安全,同步代码块)

class Singleton{
    //防止被实例化
    private Singleton(){

    }

    private static Singleton instance;

    //提供一个静态方法,返回创建返回实例对象
    public static  Singleton getInstance(){
       if(instance==null) {
           synchronized (Singleton.class) {
               instance = new Singleton();
           }
       }
       return instance;
    }

}

4.懒汉式(双重检查)

class Singleton{
    //防止被实例化
    private Singleton(){

    }

    private static volatile Singleton instance;

    //提供一个静态方法,返回创建返回实例对象
    public static Singleton getInstance(){
       if(instance==null) {
           synchronized (Singleton.class) {
               if (instance==null) {
                   instance = new Singleton();
               }
           }
       }
       return instance;
    }

}

注意:前三种在开发中,不推荐使用,第一种方法会有线程安全问题,第二,三种效率问题。而双重检查解决了前三的问题,线程安全,延迟加载,效率较高。

静态内部类

class Singleton{
    //防止被实例化
    private Singleton(){

    }

    //静态内部类
    private static class SingletonInstance{
        private static final Singleton INStANCE = new Singleton();
    }

    //返回创建的对象
    public Singleton getInstance(){
        return SingletonInstance.INStANCE ;
    }
}

注意:静态内部类方式在Singleton类被加载时并不会立即实例化,而在调用getInstance方法,才会加载SingletonInstance类,完成实例化。避免了线程不安全,利用静态内部类特点实现延迟加载,效率高。

枚举

1.使用枚举类:

enum  Singleton{
    INSTANCE;
}

注意:使用JDK1.5中添加的枚举类来实现单例对象,不仅能避免多线程问题,还能防止反序列化重新创建新的对象。

总结

单例模式保证系统内部该类对象只有一个,节省了系统资源。在需要频繁进行创建对象喝销毁对象,创建对象时消耗时过多或者消耗资源过多。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值