zoukankan      html  css  js  c++  java
  • 单例模式的五种实现模式

    这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

    注意:

    • 1、单例类只能有一个实例。

    • 2、单例类必须自己创建自己的唯一实例。

    • 3、单例类必须给所有其他对象提供这一实例。

    一,饿汉式

    线程安全,调用效率高,但不能延时加载

    实例:

    public class ImageLoader{ 
        //自己创建自己的唯一实例对象
         private static ImageLoader instance = new ImageLoader; 
        //构造函数为private,这样该类就不会被实例化
         private ImageLoader(){} 
        //获取唯一可用对象
         public static ImageLoader getInstance(){  
              return instance;  
          } 
        //其他方法
        ...
    }

    一上来就把单例对象创建出来了,要用的时候直接返回即可,这种可以说是单例模式中最简单的一种实现方式。但是问题也比较明显。单例在还没有使用到的时候,初始化就已经完成了。也就是说,如果程序从头到位都没用使用这个单例的话,单例的对象还是会创建。这就造成了不必要的资源浪费。所以不推荐这种实现方式。

    二,懒汉式

    线程安全,调用效率不高,但是能延时加载

    线程不安全

    实例:

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

    线程安全

    这种方式具备很好的 lazy loading,能够在多线程中很好的工作,但是,效率很低,99% 情况下不需要同步。 优点:第一次调用才初始化,避免内存浪费。 缺点:必须加锁 synchronized 才能保证单例,但加锁会影响效率。 getInstance() 的性能对应用程序不是很关键(该方法使用不太频繁)。

    实例:

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

    三,Double CheckLock

    这种方式采用双锁机制,安全且在多线程情况下能保持高性能。 getInstance() 的性能对应用程序很关键。

    实例

    public class Singleton {  
        private volatile static Singleton singleton;  
        private Singleton (){}  
        public static Singleton getSingleton() {  
        if (singleton == null) {  
            synchronized (Singleton.class) {  
            if (singleton == null) {  
                singleton = new Singleton();  
            }  
            }  
        }  
        return singleton;  
        }  
    }

    四,静态内部类

    线程安全,调用效率高,可以延时加载

    public class SingletonDemo3 {
         
        private static class SingletonClassInstance{
            private static final SingletonDemo3 instance=new SingletonDemo3();
        }
         
        private SingletonDemo3(){}
         
        public static SingletonDemo3 getInstance(){
            return SingletonClassInstance.instance;
        }
         
    }

    五,枚举类

    线程安全,调用效率高,不能延时加载

    public enum SingletonDemo4 {
         
        //枚举元素本身就是单例
        INSTANCE;
         
        //添加自己需要的操作
        public void singletonOperation(){     
        }
    }
  • 相关阅读:
    flock对文件锁定读写操作的问题 简单
    hdu 2899 Strange Fuction(二分)
    hdu 2199 Can you solve this equation? (二分)
    poj 3080 Blue Jeans (KMP)
    poj 2823 Sliding Window (单调队列)
    poj 2001 Shortest Prefixes (trie)
    poj 2503 Babelfish (trie)
    poj 1936 All in All
    hdu 3507 Print Article (DP, Monotone Queue)
    fzu 1894 志愿者选拔 (单调队列)
  • 原文地址:https://www.cnblogs.com/csushl/p/12718273.html
Copyright © 2011-2022 走看看