单例模式

来源:互联网 发布:淘宝的倩碧官方旗舰店 编辑:程序博客网 时间:2024/04/28 04:51

介绍


        单例模式是应用最广的模式之一,也可能是很多初级工程师唯一会使用的设计模式,在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个全局对象,这样有利于我们协调系统整体的行为。

        也就是确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。


实现方式


    懒汉模式(线程不安全)

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

        这种写法lazy loading很明显,但是致命的是在多线程不能正常工作。

    懒汉模式(线程安全)

public class Singleton {      private static Singleton instance;      private Singleton (){}      public static synchronized Singleton getInstance() {      if (instance == null) {          instance = new Singleton();      }      return instance;      }  }
        这种方式的优点是单例模式只有在使用时才会被实例化,节约了资源,并且线程安全,每次使用都要同步开销,反应稍慢,性能差。

    饿汉模式

public class Singleton {      private static Singleton instance = new Singleton();      private Singleton (){}      public static Singleton getInstance() {      return instance;      }  }
        这种方式基于classloder机制避免了多线程的同步问题,不过,instance在类装载时就实例化,虽然导致类装载的原因有很多种,在单例模式中大多数都是调用getInstance方法, 但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化instance显然没有达到lazy loading的效果。

    饿汉模式(变种)

public class Singleton {      private Singleton instance = null;      static {      instance = new Singleton();      }      private Singleton (){}      public static Singleton getInstance() {      return this.instance;      }  }
        表面上看起来差别挺大,其实变种实际没变化,都是在类初始化即实例化instance。

    Double Check Lock(DCL)实现单例

public class Singleton {      private static Singleton instance;      private Singleton (){}      public static Singleton getInstance() {      if (instance == null) {          synchronized (Singleton.class){             if (instance == null) {                     instance = new Singleton();              }        }    }      return instance;      }  }
        DCL的优点:资源利用率高,第一次执行才会被实例化,效率高
        缺点:第一次加载时反应慢,因为过多判断和同步锁,而且由于Java内存模型的原因偶尔会失败。

  静态内部类

public class Singleton {      private static class SingletonHolder {          private static final Singleton mInstance = new Singleton();      }      private Singleton (){}      public static final Singleton getInstance() {          return SingletonHolder.mInstance;      }  }
        第一次调用getInstance方法会导致虚拟机加载SingIetonHolder类,这种方式不仅能够确保线程安全,也能保证单例对象的唯一性,同时也延迟了单例的实例化,节省了资源,推荐使用静态内部类实现方式

    枚举单例

public enum Singleton {      INSTANCE;      public void whateverMethod() {      }  }
        这种方式是Effective Java作者Josh Bloch 提倡的方式,它不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象,可谓是很坚强的壁垒啊,不过,个人认为由于1.5中才加入enum特性,用这种方式写不免让人感觉生疏,在实际工作中,我也很少看见有人这么写过。

    使用容器实现

public class SingletonManager {private static Map<String,Object> objMap = new HashMap<String, Object>();private SingletonManager(){}public static void registerService(String key,Object instance){if(!objMap.containsKey(key)){objMap.put(key, instance);}}public static Object getService(String key){return objMap.get(key);}}

      在程序的初始,将多种单例类注入到一个统一的管理类中,在使用时根据key获取对象对应类型的对象。这种方式使得我们可以管理多种类型的单例,并且在使用时可以通过统一的接口进行获取操作,降低了用户的使用成本,也对用户隐藏了具体实现,降低了耦合度。




1 0