单例工具-Singleton

为什么会有这个类

平常我们使用单例不外乎两种方式:

  • 在对象里加个静态方法getInstance()来获取。此方式可以参考 【转】线程安全的单例模式 这篇博客,可分为饿汉和饱汉模式。
  • 通过Spring这类容器统一管理对象,用的时候去对象池中拿。Spring也可以通过配置决定懒汉或者饿汉模式说实话我更倾向于第二种,但是Spring更注重的是注入,而不是拿,于是我想做Singleton这个类,维护一个单例的池,用这个单例对象的时候直接来拿就可以,这里我用的懒汉模式。我只是想把单例的管理方式换一种思路,我希望管理单例的是一个容器工具,而不是一个大大的框架,这样能大大减少单例使用的复杂性。

使用

  1. /**
  2. * 单例样例
  3. * @author loolly
  4. *
  5. */
  6. public class SingletonDemo {
  7. /**
  8. * 动物接口
  9. * @author loolly
  10. *
  11. */
  12. public static interface Animal{
  13. public void say();
  14. }
  15. /**
  16. * 狗实现
  17. * @author loolly
  18. *
  19. */
  20. public static class Dog implements Animal{
  21. @Override
  22. public void say() {
  23. System.out.println("汪汪");
  24. }
  25. }
  26. /**
  27. * 猫实现
  28. * @author loolly
  29. *
  30. */
  31. public static class Cat implements Animal{
  32. @Override
  33. public void say() {
  34. System.out.println("喵喵");
  35. }
  36. }
  37. public static void main(String[] args) {
  38. Animal dog = Singleton.get(Dog.class);
  39. Animal cat = Singleton.get(Cat.class);
  40. //单例对象每次取出为同一个对象,除非调用Singleton.destroy()或者remove方法
  41. System.out.println(dog == Singleton.get(Dog.class)); //True
  42. System.out.println(cat == Singleton.get(Cat.class)); //True
  43. dog.say(); //汪汪
  44. cat.say(); //喵喵
  45. }
  46. }

总结

大家如果有兴趣可以看下这个类,实现非常简单,一个HashMap用于做为单例对象池,通过newInstance()实例化对象(不支持带参数的构造方法),无论取还是创建对象都是线程安全的(在单例对象数量非常庞大且单例对象实例化非常耗时时可能会出现瓶颈),考虑到在get的时候使双重检查锁,但是并不是线程安全的,故直接加了synchronized做为修饰符,欢迎在此给出建议。