?? 如何在子類中實現singleton模式.txt
字號:
如何在子類中實現singleton模式
http://www.umlchina.com/best/g33/u1149115.htm
作者 內容
henry_zhou 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
我現在有很多類都是從一個父類(抽象類)繼承下來的,而且所有這些子類都要實現成singleton模式,不知如何實現?
03/06/23 15:36 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 該怎么實現就怎么實現.
--------------------------------------------------------------------------------
到處都有現成的代碼.有什么問題嗎?
03/06/23 16:41 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
henry_zhou 回復: 該怎么實現就怎么實現.
--------------------------------------------------------------------------------
問題是我不能每個子類里面都實現一遍
GetInstance() {
if( _instance == null ) _instance = new ###();
return _instance;
}
吧,看看腳本都基本一樣,我想應該有一種比較好的實現方式
03/06/23 16:45 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 該怎么實現就怎么實現.
--------------------------------------------------------------------------------
虛"構造"函數,宏,模板等等都可以解決你的問題.
03/06/23 16:54 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
babituo 也許設計有問題。能列出幾個子類的名稱嗎?
--------------------------------------------------------------------------------
03/06/23 17:47 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
henry_zhou 首先感謝各位的討論,我來明確一下我的問題
--------------------------------------------------------------------------------
我的實際問題是這樣的:現在很多系統中都有下拉選擇這種東西,系統中使用代碼、而給用戶看到的是名稱,如“是/否”、“男/女”、“學歷”等,很多程序中到處都有這種代碼/名稱的引用,很分散不好管理,我現在把所有這些都封裝到類中,每一組對應一個類,這樣管理、修改就方便了,因為這些類其實基本內容、操作都一樣,只是分類數、取值不同而已,所以都從一個抽象父類來繼承,另一方面這些類的同一個類的所有實例其實都完全相同做成單態是非常適合的。所以就有了我上面提到的問題
smilemac提出的使用虛構造函數,恕我愚鈍沒能實現出來,還請多賜教!
03/06/24 17:47 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
_nil 設計可能有點問題
--------------------------------------------------------------------------------
不清楚你的上下文,也許做一個misc類,將這個類做成singleton更好一些,或者使用靜態創建的方法也可能比你現在的想法好,不需要lazy evalution.
03/06/24 19:49 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
you can define an base template class for singleton purpose,then each subclass inherited not only from its base class(es), but also from this singleton base--by protected or whatever.
by the way: couldconstructor be virtual?:)-- funny thing.
lots of guys here like to talk off without lifting a fingertip:),pity
03/06/24 20:16 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
呵呵,看來還有人不知道虛構造函數是什么,去查查書吧。
03/06/24 20:52 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 同意的說
--------------------------------------------------------------------------------
03/06/24 20:54 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
sorry, I dont know, would u please tell me what is that in english?
03/06/24 20:59 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 首先感謝各位的討論,我來明確一下我的問題
--------------------------------------------------------------------------------
這樣的設計我覺得不是很好。你覺得呢?
03/06/24 21:01 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
virtual constructor.
03/06/24 21:04 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
any further description?
I dont know how to implement singleton pattern by virtual constructor.
is it that mean registry, singleton-factory building ?
thanks
03/06/24 21:49 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
thanks a lot, finally I find that virtual constructor means abstract factory pattern, sorry for my ignorance.
03/06/24 21:56 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
actually I got two different alias about virtual constructor, abstract factory and factory methos, are they both share the same alias?
virtual constructor is a good solution to single subclass, but it is not the perfect one, actually I think single-template inheritance is better, any idea?
03/06/24 22:02 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
According to Bjarne's definition, I think the virtual constructor should mean the factory methos.
If C++, the template should be always the best solution to implement singleton in most cases, including this problem. But if considerate Java or other which do not support template, I think the virtual-constructor-likes solution maybe be the only way to tackle this problem.
03/06/24 22:30 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
never mind. maybe virtual constructor is truly not a well-known term. But I can not find another suitable name at language level to replace it. Anyway, virtual-constructor is a formal term for such functions.
03/06/24 23:34 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
that is it. but it is not the only way.
03/06/25 12:44 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
really? How ot do?
03/06/25 12:59 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
for example singleton factory + object registry + pair of (name,object), isn't that ok?
others such as class loading ..., also could be used .
03/06/25 13:02 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
Yes, many method. But does java support functor or function pointer?
I am not familiar with java.
03/06/25 13:18 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
why bother of functor?
isn't reference a better choice?
03/06/25 13:22 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
gonna go get some zzzzs, talk to u guys later
03/06/25 13:24 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
smilemac 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
sorry, i mean generic functor. otherwise how do u register if I
don't want to use class dynamic load. Can java's variable refer to a
member function? If so, it seems to violate the java's pure oo
principle.
03/06/25 14:13 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
xlm2000 不好意思我不知道怎樣用FactoryMethod去實現Singleton,因為Singleton的
--------------------------------------------------------------------------------
03/06/25 16:20 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
xlm2000 不好意思我不知道怎樣用FactoryMethod去實現Singleton,因為Singleton的instance是個靜態函數
--------------------------------------------------------------------------------
怎樣才能使用到多態?smilemac能否說得詳細些,謝謝
03/06/25 16:23 酷帖! 臭帖! 回復
酷帖評價: 臭帖評價:
返回頁首
frankwoo 回復: 如何在子類中實現singleton模式?
--------------------------------------------------------------------------------
why do you still need a functor?
why cant I just register a object after I sucessfully created that object? cant I register that object to a collector for instance?
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -