作者LPH66 (IWH68S0XZ8M89)
看板Programming
标题Re: [问题]请问这样的 Java Class 算不算是一个 Si …
时间Fri Dec 21 04:03:33 2007
※ 引述《[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实作的则是一开始就占空间了
第二点说的是用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