作者fourdollars (四元)
看板Programming
标题Re: [问题]请问这样的 Java Class 算不算是一个 Si …
时间Wed Jan 9 23:23:23 2008
※ 引述《LPH66 (IWH68S0XZ8M89)》之铭言:
: ※ 引述《[email protected] (四元)》之铭言:
: : 这个 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