Re: [問題]請問這樣的 Java Class 算不算是一個 Si …
※ 引述《LPH66 (IWH68S0XZ8M89)》之銘言:
: ※ 引述《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實作的則是一開始就佔空間了
可是下面這行就是在初始的狀態下不佔空間啊... @_@a
private static Process process = null;
另外 waitForTermination() 裡面也有 process = null;
: 第二點說的是用static實作的不能實作任何"有東西的"interface
: 這裡"有東西的"的意思是這個interface要求要實現某些成員函式
: 但是用static實作的卻不能有任何成員函式 只能有靜態(static)函式
Java 裡面的 class function 也可以當做 member function 來使用~ @_@a
SingletonProcess tmp;
tmp.runCommand("dir");
tmp.waitForTermination();
還是不懂為何不能算是 Singleton ... @_@a
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.104.128.216
討論串 (同標題文章)