⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 如何在子类中实现singleton模式.txt

📁 一些关于UML的经典讨论
💻 TXT
📖 第 1 页 / 共 2 页
字号:
如何在子类中实现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 + -