Re: [問題]請問這樣的 Java Class 算不算是一個 Si …
※ 引述《qrtt1.bbs@bbs.sayya.org (foolish)》之銘言:
: 實作 Singleton Pattern 簡單的說就是
: 控管 constructor 讓外界無法由 new 的方式產生任意個物件
: public final class SingletonEx {
: static SingletonEx self = new SingletonEx();
: private SingletonEx() {
: }
: public static SingletonEx getInstance(){
: return self;
: }
: public static void main(String[] args) {
: // reference 都應該要是一樣的
: assert(SingletonEx.getInstance() == SingletonEx.getInstance());
: }
: }
非class不可嗎?
為什麼不能作成只能存在一個instance.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.160.211.156
→
01/10 23:20, , 1F
01/10 23:20, 1F
→
01/10 23:54, , 2F
01/10 23:54, 2F
→
01/11 01:25, , 3F
01/11 01:25, 3F
→
01/11 01:26, , 4F
01/11 01:26, 4F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 5 之 16 篇):