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

» JWorld@TW » Software Design » Effective Java  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
話題被移動
該話題已被移動 - morchory , 2003-10-03 01:40
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
無標籤
作者 [Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [精華]
metavige

麥塔.米奇

版主

發文: 2133
積分: 10
於 2003-10-01 15:24 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作法一:
1
2
3
4
5
6
7
8
9
10
// Singleton with final field
public class Browser {
  public static final Browser INSTANCE = new Browser();
 
  private Browser() {
    ....
  }
 
  ... // Remainder omitted
}
優點:比較清楚表達這個 class 是 singleton

作法二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Singleton with static factory
public class Browser {
  private static final Browser INSTANCE = new Browser();
 
  private Browser() {
    ....
  }
 
  public static Browser getInstance() {
    return INSTANCE;
  }
 
  ... // Remainder omitted
}
優點: 較有彈性

這裡還有提到有關於 singleton class serializable 的話題
這應該會在條款57會提到

這個條款
書裡面用程式碼來描述
十分的容易理解
但是詳細的部分, 可能大家還是要在書裡面才看的到
我這邊就不多說啦
--------------------------------------------------------------------------------
不過我連程式碼都是抄書的, 有點罪惡感∼Sleepy


metavige edited on 2003-10-01 15:54
reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:metavige]
ymshin





發文: 277
積分: 4
於 2003-10-01 15:42 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
可以把貓王改成 Browser~~~ Big Smile

reply to postreply to post
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:ymshin]
metavige

麥塔.米奇

版主

發文: 2133
積分: 10
於 2003-10-01 15:55 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ymshin wrote:
可以把貓王改成 Browser~~~ Big Smile


好意見 Thumbs up


reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:metavige]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2003-10-01 21:48 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
Singleton: 2 ways to create the singleton object instance,
(1) Like demo program, declare a static final field and initialize immediately.
(2) Create on getInstance method.

Method (2) allows you do "lazy" creation, created only when you needed.

There are also thread safety issue.
Sometime, if you choice method (2), you may need sychronized to make sure thread safe.

My question is, if you choice method (1), do you still need to worry about thread safety problem ?
It seems to me that even on "static statement", you are not guarantee thread-safe.


reply to postreply to post
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:T55555]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2003-10-01 22:21 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
T55555 wrote:
My question is, if you choice method (1), do you still need to worry about thread safety problem ?
It seems to me that even on "static statement", you are not guarantee thread-safe.


It IS thread-safe,針對 singleton 的建立這一點來說。
static field 所參考的 singleton object 是在 class constructor 裡建立,而合格的 JRE 應該要能保證每個 class 的 class constructor 只執行一次。至於 Browser object 的使用上是不是 thread-safe 要看怎麼他是怎麼設計的。


Duncan edited on 2003-10-03 13:38
reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:metavige]
ingramchen

Web monkey



發文: 479
積分: 12
於 2003-10-01 22:45 user profilesend a private message to usersend email to ingramchenreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
參考 javaworld 的文章:

http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

舉凡 serialization, threadsafe 到套用 registry, reflection
該有的都有了!


reply to postreply to post
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:T55555]
etman





發文: 15
積分: 0
於 2003-10-29 10:40 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
T55555 wrote:
Singleton: 2 ways to create the singleton object instance,
(1) Like demo program, declare a static final field and initialize immediately.
(2) Create on getInstance method.

Method (2) allows you do "lazy" creation, created only when you needed.

There are also thread safety issue.
Sometime, if you choice method (2), you may need sychronized to make sure thread safe.

My question is, if you choice method (1), do you still need to worry about thread safety problem ?
It seems to me that even on "static statement", you are not guarantee thread-safe.

當你使用第二種方式去實作Singleton,亦即代表您產生的instance不是定義為final的,這很危險,若此類別儲存的是某些系統上的必要資訊,由於這個參考是可修改的,所以當傳出去後,再將這個參考指定為null...我想會發生蠻嚴重的事,當然,或許你可以預期沒有人會做這樣的事情,但事事難預料丫...這份instance就交由Garbage collection處理去了...如此,有沒有Thread-safe反而不是很重要的事情~~利用第一種方式,如上所說是絕對確保不會有Concurrent issue的!!


etman edited on 2003-10-29 11:29
reply to postreply to post
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:etman]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2003-10-30 16:13 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
etman wrote:
當你使用第二種方式去實作Singleton,亦即代表您產生的instance不是定義為final的,這很危險,若此類別儲存的是某些系統上的必要資訊,由於這個參考是可修改的,所以當傳出去後,再將這個參考指定為null...我想會發生蠻嚴重的事,當然,或許你可以預期沒有人會做這樣的事情,但事事難預料丫...這份instance就交由Garbage collection處理去了...


這應該要怪 reflection 不尊重 access privilege,否則你怎麼有辦法去更動一個非公開的 field。


reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:Duncan]
etman





發文: 15
積分: 0
於 2003-10-30 16:18 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
Duncan wrote:
這應該要怪 reflection 不尊重 access privilege,否則你怎麼有辦法去更動一個非公開的 field。

哦...所以你在這裡抱怨不需要自己去解決囉??~~當你在運用reflection的好處是,怎麼沒有怪他呢??~呵呵


reply to postreply to post
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:etman]
Duncan

還隱隱作痛

版主

發文: 7816
積分: 39
於 2003-10-30 18:53 user profilesend a private message to usersend email to Duncanreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
etman wrote:
哦...所以你在這裡抱怨不需要自己去解決囉??~~當你在運用reflection的好處是,怎麼沒有怪他呢??~呵呵


別這麼說嘛Embaressed,這不過是一種語氣罷了!

但是如果真要考慮 reflection 進來,姑且不論你使用了什麼 pattern 或技巧,你用 Java PL 寫出來的 stateful 元件,十之八九都是不合格的。

只要你的 class 有內部狀態,我都可以用 reflection 來介入而破壞其 invariant 性質,出槌就在所難免了。


reply to postreply to post

給我
辣味豆腐 其餘免談
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:Duncan]
etman





發文: 15
積分: 0
於 2003-10-30 22:32 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
Duncan wrote:
別這麼說嘛Embaressed,這不過是一種語氣罷了!

但是如果真要考慮 reflection 進來,姑且不論你使用了什麼 pattern 或技巧,你用 Java PL 寫出來的 stateful 元件,十之八九都是不合格的。

只要你的 class 有內部狀態,我都可以用 reflection 來介入而破壞其 invariant 性質,出槌就在所難免了。

呵呵,故意和不經意的差別你應該先思考一下...別動不動就提供想法教別人搞破壞...reflection本就是讓人又愛又恨的東西了


reply to postreply to post
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:etman]
metavige

麥塔.米奇

版主

發文: 2133
積分: 10
於 2003-10-31 09:39 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
etman wrote:
呵呵,故意和不經意的差別你應該先思考一下...別動不動就提供想法教別人搞破壞...reflection本就是讓人又愛又恨的東西了

破壞的恰到好處
那就是好用了~


reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:metavige]
etman





發文: 15
積分: 0
於 2003-10-31 10:06 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
metavige wrote:
破壞的恰到好處
那就是好用了~

不清楚你的意思,內部狀態被破壞~還可以弄到洽到好處...這句話似乎廣義了些~~


reply to postreply to post
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:etman]
metavige

麥塔.米奇

版主

發文: 2133
積分: 10
於 2003-10-31 13:12 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
etman wrote:
不清楚你的意思,內部狀態被破壞~還可以弄到洽到好處...這句話似乎廣義了些~~

這是玩笑話
不用太在意

我指的是破壞封裝性
而不是破壞狀態

因為我覺得過度的封裝會有效率上的影響
所以才有此言


reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:metavige]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-07-02 14:01 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
在2nd edition提到使用enum singleton能更安全

1
2
3
4
public enum Elvis{
  INSTANCE;
  public void methodXXX(){};
}


http://en.wikipedia.org/wiki/Singleton_pattern#The_Enum-way

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:[Effective Java] 條款2 : 以 private 建構式厲行 singleton (單件)性質 [Re:koji]
YuLimin

简单就是美

版主

發文: 134
積分: 0
於 2008-10-05 21:51 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
又一种新的单例模式:)

reply to postreply to post
 当兵不后悔!后悔不当兵! 
超越黎明时空,追逐时代潮流!
人,是要靠自己的!简单就是美!
我的Java:http://www.Java2Class.net
我的Blog:http://YuLimin.JavaEye.com
» JWorld@TW »  Software Design » Effective Java

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8