JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Software Design » GoF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [DP]Singleton pattern, Double-checked Locking pattern (by Biologic) [精華]
popcorny

Jakarta 2%

版主

發文: 752
積分: 20
於 2003-07-19 16:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
顧名思義, 就是系統中該 class 只建立一個 object: Class.

主要是利用 class 中 constructor 的 accessblity. e.g.
1
2
3
4
5
6
7
8
9
10
class CLASS {
    static private CLASS obj = null;
    private CLASS() {}
 
    static public CLASS getInstance() {
        if (obj == null)
            obj = new CLASS();
        return obj;
    }
}


1
2
3
4
class CLASS {
    static public final CLASS obj = new CLASS();
    private CLASS() {}
}


把 constructor 設為 private 讓其他 client 無法直接建立物件. client 如果要取得這單一物件則必須經由 static public method 來取的物件.

當然你也可以把數量設定為你需要的數量, 不一定只有一個. 只不過還叫 singleton 可能就不太適合了. 但如果你想要把物件定為某些 class 可以用, 那就應該問一下 popcorny 答案了....

UML: 不清楚對不對, 大家作參考吧...


popcorny edited on 2003-07-20 11:57
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
31319 [精華] [DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 660 2003-07-19 16:13
29551 Re:[DP]Singleton, Double-check Locking(by Biologic popcorny 1727 2003-07-19 16:17
29281 Re:[DP]Singleton, Double-check Locking(by Biologic Biologic 102 2003-07-19 16:33
29457 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) ymshin 584 2003-07-20 04:01
29321 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) Biologic 1007 2003-07-20 09:47
28986 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) ymshin 591 2003-07-20 10:47
28983 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) Biologic 265 2003-07-20 12:47
28946 Re:[DP]Singleton pattern, Double-check Locking pattern (by Biologic) ymshin 350 2003-07-20 13:58
29765 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 3208 2003-07-20 13:49
29660 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 1942 2003-07-20 14:03
29048 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 266 2003-07-20 15:09
29083 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) ymshin 1054 2003-07-20 15:41
28880 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) browser 764 2003-07-20 16:01
28979 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) ymshin 183 2003-07-20 16:04
29050 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 1214 2003-07-20 16:18
29026 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) ymshin 1738 2003-07-20 17:00
29104 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) william 320 2003-08-03 02:36
29159 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 1843 2003-07-20 17:28
29178 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 1218 2003-07-20 18:20
29021 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 2402 2003-07-20 14:09
29054 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) popcorny 684 2003-07-20 16:34
29001 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 333 2003-07-20 20:23
28897 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 571 2003-07-20 21:39
28970 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) koji 250 2003-07-20 22:14
28867 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) Biologic 380 2003-07-20 22:45
28014 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) plutotw 526 2004-09-02 09:47
28240 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) alien 2115 2004-09-03 09:01
28634 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) plutotw 725 2004-09-03 21:03
25796 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) zanyking 672 2005-08-24 13:43
12915 Re:[DP]Singleton pattern, Double-checked Locking pattern (by Biologic) shukae 876 2010-12-17 14:42
» JWorld@TW »  Software Design » GoF

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8