美文网首页
单例模式

单例模式

作者: 東東_Katrina | 来源:发表于2019-06-23 11:03 被阅读0次

一、单例模式的概念
单例模式是指整个应用生命中期内只有一个实例。单例模式是一种广泛使用的设计模式,能够避免对象的重复创建,减少内存开销。

二、单例模式与静态类的区别

三、如何实现单例模式
1、饿汉模式

public class ImageLoader {
   public static ImageLoader mInstance = new ImageLoader();

   private ImageLoader() {
   }

   public static  ImageLoader getInstance() {
       return mInstance;
   }
}

2、懒汉模式

public class ImageLoader {
    public static ImageLoader mInstance;

    private ImageLoader() {
    }

    public static  ImageLoader getInstance() {
        if (mInstance == null) {
            mInstance = new ImageLoader();         
        }
        return mInstance;
    }
}
public class ImageLoader {
    public static ImageLoader mInstance;

    private ImageLoader() {
    }

    public static synchronized ImageLoader getInstance() {
        if (mInstance == null) {
            mInstance = new ImageLoader();         
        }
        return mInstance;
    }
}
public class ImageLoader {
    public static ImageLoader mInstance;

    private ImageLoader() {
    }

    public static ImageLoader getInstance() {
        synchronized(ImageLoader.class) {
            if (mInstance == null) {
                mInstance = new ImageLoader();
           }
        }
        return mInstance;
    }
}
public class ImageLoader {
    public static ImageLoader mInstance;

    private ImageLoader() {
    }

    public static ImageLoader getInstance() {
        if (mInstance == null) {
            synchronized(ImageLoader.class) {
                mInstance = new ImageLoader();
           }
        }
        return mInstance;
    }
}
public class ImageLoader {
    public static ImageLoader mInstance;

    private ImageLoader() {
    }

    public static ImageLoader getInstance() {
        if (mInstance == null) {
            synchronized(ImageLoader.class) {
                if (mInstance == null) {
                    mInstance = new ImageLoader();
               }
           }
        }
        return mInstance;
    }
}

3、静态内部类

public class ImageLoader {
    private ImageLoader() {
    }

    public static ImageLoader getInstance() {
        return ImageLoaderInner.mInstance;
    }
    
    private static class ImageLoaderInner {
        private static ImageLoader mInstance = new ImageLoader();
    }
}

参考文章:
1、Java实现单例的5种方式

相关文章

  • 【设计模式】单例模式

    单例模式 常用单例模式: 懒汉单例模式: 静态内部类单例模式: Android Application 中使用单例模式:

  • Android设计模式总结

    单例模式:饿汉单例模式://饿汉单例模式 懒汉单例模式: Double CheckLock(DCL)实现单例 Bu...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • 设计模式之单例模式详解

    设计模式之单例模式详解 单例模式写法大全,也许有你不知道的写法 导航 引言 什么是单例? 单例模式作用 单例模式的...

  • Telegram开源项目之单例模式

    NotificationCenter的单例模式 NotificationCenter的单例模式分析 这种单例模式是...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • IOS单例模式的底层原理

    单例介绍 本文源码下载地址 1.什么是单例 说到单例首先要提到单例模式,因为单例模式是单例存在的目的 单例模式是一...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • 单例模式

    单例模式1 单例模式2

  • java的单例模式

    饿汉单例模式 懒汉单例模式

网友评论

      本文标题:单例模式

      本文链接:https://www.haomeiwen.com/subject/apcsqctx.html