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

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

📁 一些关于UML的经典讨论
💻 TXT
📖 第 1 页 / 共 2 页
字号:
 
 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?
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -