Programming 板


LINE

※ 引述《fourdollars (四元)》之銘言: : ※ 引述《LPH66 (IWH68S0XZ8M89)》之銘言: : : 個人以為這不是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實作的則是一開始就佔空間了 : 可是下面這行就是在初始的狀態下不佔空間啊... @_@a : private static Process process = null; : 另外 waitForTermination() 裡面也有 process = null; 這裡說的不是instance 而是這個class本身 注意它不只說不佔記憶體 它是說 "requiring no memory or resources until needed" 以java來說你可以把它寫成讓classloader會在用到時才把它載入 而static class的話無論如何classloader會在一開始就把它載入了 (維基那頁下面有個這樣的範例) : : 第二點說的是用static實作的不能實作任何"有東西的"interface : : 這裡"有東西的"的意思是這個interface要求要實現某些成員函式 : : 但是用static實作的卻不能有任何成員函式 只能有靜態(static)函式 : Java 裡面的 class function 也可以當做 member function 來使用~ @_@a : SingletonProcess tmp; : tmp.runCommand("dir"); : tmp.waitForTermination(); 話說一般的Singleton不會寫成這樣吧... 它所謂不能實作"有東西的"interface的意思是 那些interface裡定義了實作它的人該要有某些member method 而你的class裡只能有static method (而這是因為你必然得要用SingletonProcess.runCommand("dir");這種方式呼叫 這使得runCommand必然得是個static method) 舉個和你寫的很像的東西的好了: java.lang.Math 它不是Singleton 但其他方面卻和你寫的東西很像: 呼叫時要用 Math.abs(value); Math.pow(a,b); 等等 而它的這些所有method都是static 當然 就如你所說它也能這樣用: Math mymath; c=mymath.pow(a,b); 但這正表明了Math這個class不是一個Singleton : 還是不懂為何不能算是 Singleton ... @_@a 重新看過一次你的code之後 最根本的問題應該是: 你的class能夠隨時產生實體 而能夠隨時產生實體的class最一開始就不是Singleton了 就如同java.lang.Math的例子一樣 它也不是個Singleton (維基那頁開宗明義就說了 "the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.") -- '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.250.80 ※ 編輯: LPH66 來自: 140.112.250.80 (01/10 01:27)
1F:推 oohay:解釋得好 59.112.227.50 01/10 01:50







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP