作者 内容
 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?

 03/06/25 21:41 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 如何在子类中实现singleton模式?
 

That's not lazy eval singleton, which is the point of Henry_zhou's problem. For static initialization, maybe the registerring is not neccessory too.

 03/06/25 21:54 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 frankwoo  回复: 如何在子类中实现singleton模式?
 

why not lazy ?
actually I can build any object whenever I need to, but still can have more than one object in same family .

 03/06/25 22:01 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 如何在子类中实现singleton模式?
 

I am puzzled with you. So how you create instance first time? If you do not register the object before it first creation, how do you get the type?

 03/06/25 22:10 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 frankwoo  回复: 如何在子类中实现singleton模式?
 

I creat the object through a factory, in the makeXXX() function of this factory class, first I lookup the object , if is is there in register, then just return that ,otherwise , new an object and then register that object. each object has an unique name .
what does mean I need a type, sure in implementation of this factory I need to know the type otherwise how could I know which kind of object is gonna be "newed".
actually the client of this factory should know whatkind of object they gonna creat, so is that a problem?

 03/06/25 22:18 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 如何在子类中实现singleton模式?
 

:)
You have to have each singleton subclass accompany with a factory or have to introduce a long swich-case sentance. All both are not so good design, to make the problem complex.

That's why I ask whether java support generic functor, which is the only thing possibly be supported by new java by guessing.

 03/06/25 22:35 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 xlm2000  回复: 如何在子类中实现singleton模式?
 

hi, 请看看我在下面的回复好吗http://umlchina.smiling.com/group/posts/view_forum.ecgi?group_id=9986&res_message_id=1149219

 03/06/25 22:45 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 frankwoo  回复: 如何在子类中实现singleton模式?
 

You have to have each singleton subclass accompany with a factory or have to introduce a long swich-case sentance.

--nop, not for each singleton class, but for a family of singleton classes.
if you can get a singleton object by invoking singleClass.instance(), then what is the difference of by doing singleFactory.builClassName() except for access modifier?


 

 03/06/25 22:58 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 如何在子类中实现singleton模式?
 

Are you trying to solve the orignal problem--how to share the same singleton patern code among a lot of subclasses? I think you be deviating from it.:)

 03/06/25 23:09 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 如何在子类中实现singleton模式?
 

是的,我的消息似乎也有点毛病,可以等一下吗?

 03/06/25 23:11 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 如何在子类中实现singleton模式?
 

Could you please show me how you write each function of singleFactory.builClassName() ?

 03/06/25 23:17 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 不好意思我不知道怎样用FactoryMethod去实现Singleton,因为Singleton的instance是个静态函数
 

你用的是什么语言?

 03/06/25 23:25 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 xlm2000  是c++
 
 03/06/25 23:26 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 是c++
 

那你用模版就可以了,看看frankwoo的第一个贴子,在C++,模版是解决此问题的最好方法。

 03/06/25 23:30 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 xlm2000  没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 
 03/06/25 23:33 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

可以,但不好,只是没有模版或泛型的一种替代方法。能不用则不用。

 03/06/25 23:41 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 xlm2000   回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

我。。仍然没有想到如何实现,首先instance()必须是个静态,而消除各子类里instance()相似代码是现在的目标,好像没法使用FactoryMethod啊

 03/06/25 23:46 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

It is not true that instance() must be a static.

 03/06/26 00:02 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 xlm2000  回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

如果不静态,那谁来生成这个对象来执行instance?而singleton的构造是保护的,越来越糊涂了,给段代码好吗

 03/06/26 00:12 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 henry_zhou  回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

就象xlm2000所说的如果不用静态函数的话势必要生成一个类对象才能调用该函数,那么也就不可能是singleton模式了,我也一直没想通。template好像C#、Java中都没有(我还没看Template模式如何在java中实现,如果java语言不支持那么实现起来可能也比较费劲)
前面_nil提到设计成misc类,我有空找找这方面的东西看看再请教!
至于这个设计的确可能存在一点问题,我现在也在想一种更好的设计方案(还有一些其他因素需要考虑),提出的问题虽然是由这个实际问题引出的,但现在已经不是为了解决这个问题了,而是我想知道如果以后要使用这种设计该如何解决。
再次感谢各位!

 03/06/26 10:23 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

please follow the below link.
http://umlchina.smiling.com/group/posts/view_forum.ecgi?group_id=9986&res_message_id=1149265

 03/06/26 14:52 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

please follow the below link.
http://umlchina.smiling.com/group/posts/view_forum.ecgi?group_id=9986&res_message_id=1149265

 03/06/26 14:52 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 henry_zhou  回复: 没错用模板没有问题,但是FactoryMethod呢?C++不行吗
 

非常感谢你写的如此详细的实现方案

 03/06/26 18:06 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 frankwoo  回复: 如何在子类中实现singleton模式?
 

pseudo code attached as follow:

actually for each family u can get the real factory from abstarct factory, here, it is not key point, so I just give an easier one with factory.--I am always lazy.:)
Java example.
class singletonFactory{
public sychronized static singletonFactory getInstance()
{
if( _instance != null)
_instance = new singletonFactory();
return _instance;
}

/** buildSingleSubClass functions from here */
public Base buildSubclass1( string subClassID) {
Base tB;
if( (tB =(Base)lookup(subClassID) ) != null )/* subclass1 not in collection*/
{
tB = new sunClass(...);
register(subClassID,tB);/* register tB to inner Collection*/
}
return tB;
}
/* for other subclass etc, the steps like afore given example*/
......................
/* just keep lookup() and register() thead safe.
private sychronized Object lookup(string ID) { look up ID in inner_collection , if find, then return that ID peer (object) , otherwise return null.;}

register( string ID, object obj) { make a pair of ID and obj, and insert them into inner_collection*/}

.........
/*Constructor*/
singletoFactory() {}

private static singletonFactory _instance = null;
private static map inner_collection=new collection_type(); /* or else collection to store the inner collection of subClass Objects;*/

}


/*notice:
defferent sub classes have different "name"--ID, that should be defined by client.
u can also move the collection outside of factory, if u can make sure it is has only one instance or define it as a class wrapper.

though it is not the perfect code, the elite point is: object registery:), notice that not just getInstance() can be non static, but also there could be no getInstance() at all.

 03/06/27 12:21 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首
 smilemac  回复: 如何在子类中实现singleton模式?
 

I think these code seem not to be right. there is no synchronizing to the new instance process but there is one to the new factory process. what's that meaning?

And I am also puzzled with that how you know which sunClass you should new? Should it be a switch-case sentance?

 03/06/27 14:24 酷帖!    臭帖!    回复  
酷帖评价:           臭帖评价:
返回页首