Singleton模式的五种实现

单例模式的特点:

  • 单例类只能有一个实例。
  • 单例类必须自己创建自己的唯一实例。
  • 单例类必须给所有其他对象提供这一实例。 -《Java与模式》

单例类可以是没有状态的,仅用做提供工具性函数的对象。既然是提供工具性函数,也就没有必要创建多个实例。

 

下面列举单例模式的几种实现:

  • 单元素的枚举类型实现Singleton – the preferred approach.enum类型是Jdk1.5引入的一种新的数据类型.其背后的基本想法:通过公有的静态final域为每个枚举常量导出实例的类。因为没有可访问的构造器,枚举类型是真正的final。既不能创建枚举类型实例,也不能对它进行扩张 。枚举类型是实例受控的,是单例的泛型化,本质上是氮元素的枚举 -《Effective Java》P129
  1. public enum Singleton{  
  2.     INSTANCE;  
  3.     public void execute(){…}  
  4.     public static void main(String[] agrs){  
  5.         Singleton sole=Singleton.INSTANCE;  
  6.         sole.execute();  
  7.     }  
  8. }  
  • 懒汉式 – Lazy initialization . 构造函数是private,只有通过Singleton才能实例化这个类。
  1. public class Singleton{  
  2.     private volatile static Singleton uniqueInstance=null;  
  3.     private Singleton(){}  
  4.     public static Singleton getInstance(){  
  5.         if(uniqueInstance==null){  
  6.             synchronized(Singleton.class){  
  7.                 if(uniqueInstance==null){  
  8.                     uniqueInstance=new Singleton();  
  9.                 }  
  10.             }  
  11.         }  
  12.         return uniqueInstance;  
  13.     }  
  14.     //other useful methods here  
  15. }  
  • 饿汉式 – Eager initialization
  1. public class Singleton{  
  2.     private static Singleton uniqueInstance=new Singleton();  
  3.     private Singleton(){}  
  4.     public static Singleton getInstance(){  
  5.         return uniqueInstance;  
  6.     }  
  7. }  
  • static block initialization
  1. public class Singleton{  
  2.     private static final Singleton instance;  
  3.           
  4.     static{  
  5.         try{  
  6.             instance=new Singleton();  
  7.         }catch(IOException e){  
  8.             thronw new RuntimeException("an error's occurred!");  
  9.         }  
  10.     }  
  11.     public static Singleton getInstance(){  
  12.         return instance;  
  13.     }  
  14.     private Singleton(){  
  15.     }  
  16. }  
  • The solution of Bill Pugh 。- From wiki <Singleton Pattern> 。通过静态成员类来实现,线程安全。
  1. public class Singleton{  
  2.     //private constructor prevent instantiation from other classes  
  3.     private Singleton(){}  
  4.       
  5.     /** 
  6.     *SingletonHolder is loaded on the first execution of Singleton.getInstance() 
  7.     *or the first access to SingletonHolder.INSTANCE,not befor. 
  8.     */  
  9.      private static class SingletonHolder{  
  10.         public static final Singleton instance=new Singleton();  
  11.      }  
  12.      private static Singleton getInstance(){  
  13.         return SingletonHolder.instance;  
  14.      }  
  15. }  

posted on   matt_chen  阅读(994)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2012年6月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
1 2 3 4 5 6 7

统计

点击右上角即可分享
微信分享提示