快速理解单件SINGLETON模式

来源:互联网 发布:手机反监控软件 编辑:程序博客网 时间:2024/05/22 06:33

SINGLETON(单件)—对象创建型模式    -- 只保存一个实例。

关键点:

1. 构造函数为私有,外部不能访问。

2. 只能通过静态函数getInstance访问,访问前判断是否已经实例化。

  1. public class Singleton {  
  2.     private Singleton() {}  
  3.     private static Singleton single=null;  
  4.     public static Singleton getInstance() {  
  5.          if (single == null) {    
  6.              single = new Singleton();  
  7.          }    
  8.         return single;  
  9.     }  
  10. }  

代码摘自 http://blog.csdn.net/hguisu/article/details/7515416


    0 0
    原创粉丝点击