Singleton Pattern

SSD 3 的最后一题竟然考的是 Singleton Pattern, 暑假见过,后来没想也没写,导致看到题目就O_O

 

好吧,再翻点东西出来搜藏...

 

设计模式之Singleton :http://www.qqread.com/soft-engineering/t320491.html

设计模式之Singleton(单态) :http://www.blogjava.net/endisoft/archive/2006/09/27/72178.html

 

考试最后一题的答案:

/**

采用了双重检验机制。 双重检验发生在第一个 IF 块上。 如果成员变量为空,则执行进入关键部分块,该块再次双重检验该成员变量。 仅在通过此最终测试后,才会实例化该成员变量。

*/

class Singleton {
 public static Singleton Instance() {
   if (_instance == null) {
    synchronized (Class.forName("Singleton")) {
     if (_instance == null) {
     _instance = new Singleton();
    }
   }
  }
return _instance;
}
protected Singleton() {
}
private static Singleton _instance = null;

 

 

posted @ 2008-11-06 20:34  yyzhang  阅读(106)  评论(0编辑  收藏  举报
无觅关联推荐,快速提升流量