Re: [問題]請問這樣的 Java Class 算不算是一個 Si …
※ 引述《FourDollars.bbs@bbs.sayya.org (四元)》之銘言:
: 這個 Sample Code 的主要目地是讓 Java 去執行一個外部程式
: 但是同一個時間內只允許一個外部程式的執行
: 如果要執行第二個外部程式就必須等到前面的外部程式結束後才能執行
: 有人跟我說這不算是 Singleton 於是我就去翻 Design Patterns (四人幫那本)
: 看來看去還是搞不清楚是哪裡有問題!? 請版上的前輩指點小弟一下 <(_ _)>
個人以為這不是Singleton
因為從頭到尾你的class連一個instance都沒有出現過
只是利用了static的共有性質達成目的
---
以下節錄英文維基: ( http://en.wikipedia.org/wiki/Singleton_pattern )
Note the distinction between a simple static instance of a class and a
singleton: although a singleton can be implemented as a static instance,
it can also be lazily constructed, requiring no memory or resources until
needed. Another notable difference is that static member classes cannot
implement an interface, unless that interface is simply a marker.
第一點是說這種class可以在要用到時才佔空間 (即沒用到就不佔)
而static實作的則是一開始就佔空間了
第二點說的是用static實作的不能實作任何"有東西的"interface
這裡"有東西的"的意思是這個interface要求要實現某些成員函式
但是用static實作的卻不能有任何成員函式 只能有靜態(static)函式
--
'Oh, Harry, dont't you see?' Hermione breathed. 'If she could have done
one thing to make absolutely sure that every single person in this school
will read your interview, it was banning it!'
---'Harry Potter and the order of the phoenix', P513
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.30.84
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 16 篇):