Singleton单身人士的收藏可能吗? [英] Is Singleton collection of Singletons possible??

查看:70
本文介绍了Singleton单身人士的收藏可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我正在编写一个商业应用程序,我希望每个特定客户只有一个客户对象的一个​​

实例(代表一个

数据库记录)正在编辑。


是否可以扩展Singleton模式来处理这个?


假设我的Customer类遵循Singleton模式(特别是

Skeet'的第4版)我在想是否添加


私有静态SomeCollectionType客户;


然后将工厂更改为


public static GetCustomer(Guid customerID)

{

//检查集合并返回现有实例如果找到

//创建新实例并返回如果不是

}


我认为我对静态和单例概念有所了解,但需要确保


Let''s say I''m writing a business app and I want there to be only one
instance of the Customer object for each particular customer (representing a
database record) being edited.

Would it be possible to extend the Singleton pattern to handle this?

Assuming that my Customer class follows the Singleton pattern (particularly
Skeet''s 4th version) I''m thinking if I add

private static SomeCollectionType customers;

and then change the factory to something like

public static GetCustomer(Guid customerID)
{
// check collection and return existing instance if found
// create new instance and return if not
}

I think I have my mind around the static and singleton concepts, but need to
make sure.

推荐答案

Daniel Billingsley <分贝********* *@NO.durcon.SPAAMM.com>写道:
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
让我们说我正在编写一个商业应用程序,我希望每个特定客户只有一个Customer对象的实例(代表

是否可以扩展Singleton模式来处理这个问题?


排序 - 然后它会被称为工厂模式:)

假设我的Customer类遵循Singleton模式(特别是
Skeet '的第4版)我在想是否添加私有静态SomeCollectionType客户;

然后将工厂更改为
public static GetCustomer(Guid customerID)
//检查收集并返回现有实例如果找到
//创建新实例并返回如果不是
}
<我认为我对静态和单例概念有所了解,但需要确保。
Let''s say I''m writing a business app and I want there to be only one
instance of the Customer object for each particular customer (representing a
database record) being edited.

Would it be possible to extend the Singleton pattern to handle this?
Sort of - it would then be called the Factory pattern though :)
Assuming that my Customer class follows the Singleton pattern (particularly
Skeet''s 4th version) I''m thinking if I add

private static SomeCollectionType customers;

and then change the factory to something like

public static GetCustomer(Guid customerID)
{
// check collection and return existing instance if found
// create new instance and return if not
}

I think I have my mind around the static and singleton concepts, but need to
make sure.




那没关系 - 但对于线程-safety你应该基本上有一个

锁定,每次* GetCustomer被调用时获取*。第四个

单身人士模式对你来说并不是很有用。第二种模式

更接近您需要使用的模式。


-

Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget

如果回复小组,请不要给我发邮件



That would be fine - but for thread-safety you should basically have a
lock which is acquired *every* time GetCustomer is called. The fourth
singleton pattern isn''t really much use to you here. The second pattern
is much closer to what you''d need to use.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


是的但我需要工厂也是一个单身人士或它击败了

的目的。所以我需要一个GetFactory(),然后我需要一个GetCustomer()。


关于锁定...帮助我解决这个问题。问题是两个

线程可以调用GetCustomer()吗?由于这不是由工厂本身的单独性质解决的,因此需要锁定。


所以我可以按照工厂的第4种模式本身然后在GetCustomer()中使用

锁。对?并不是说整个

模式中的锁定在这一点上可能很重要,因为无论如何我将在另一种方法中左右锁定

。 />

" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Yeah but I would need the Factory to be a Singleton also or it defeats the
purpose. So I need a GetFactory() and then a GetCustomer() I guess.

As to the locking... help me sort this out in my head. The problem is two
threads could call GetCustomer() right? Since that isn''t addressed by the
Singleton nature of the factory itself the locks are required.

So I could follow the 4th pattern for the factory itself and then use the
locks within GetCustomer(). Right? Not that the locking in the overall
pattern probably matters much at that point since I''m going to be locking
left and right in the other method anyway.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel Billingsley< db ********** @ NO.durcon.SPAAMM.com>写道:
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
让我们说我正在编写一个商业应用程序,我希望每个特定客户只有一个客户对象的实例
(代表正在编辑的数据库记录。

是否可以扩展Singleton模式来处理这个问题?
Let''s say I''m writing a business app and I want there to be only one
instance of the Customer object for each particular customer (representing a database record) being edited.

Would it be possible to extend the Singleton pattern to handle this?



排序 - 它将被称为工厂虽然模式:)



Sort of - it would then be called the Factory pattern though :)

假设我的Customer类遵循Singleton模式
(特别是Skeet的第4版)我在想是否添加

私有静态SomeCollectionType客户;

然后将工厂更改为类似

公共静态GetCustomer(Guid customerID)

/ / check如果找到则返回现有实例
//创建新实例并返回如果不是

我认为我对静态和单例概念有所了解,但是
需要确保。
Assuming that my Customer class follows the Singleton pattern (particularly Skeet''s 4th version) I''m thinking if I add

private static SomeCollectionType customers;

and then change the factory to something like

public static GetCustomer(Guid customerID)
{
// check collection and return existing instance if found
// create new instance and return if not
}

I think I have my mind around the static and singleton concepts, but need to make sure.



那没关系 - 但是对于线程安全,你应该基本上有一个
锁定,每次调用GetCustomer时都会获得它。第四种单身模式对你来说并不是很有用。第二种模式更接近你需要使用的模式。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



That would be fine - but for thread-safety you should basically have a
lock which is acquired *every* time GetCustomer is called. The fourth
singleton pattern isn''t really much use to you here. The second pattern
is much closer to what you''d need to use.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Daniel Billingsley< db ********** @ NO。 durcon.SPAAMM.com>写道:
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
是的,但我还需要工厂成为单身人士,否则就会失去
目的。所以我想要一个GetFactory()然后一个GetCustomer()。


嗯,工厂方法本身不一定是实例的一部分 -

你不需要单独上课。有没有什么理由不仅仅有一个

静态GetCustomer方法?

关于锁定...帮助我解决这个问题。问题是两个线程可以调用GetCustomer()吗?


是。

由于工厂本身的单独性质并未解决这个问题,因此需要锁定。


是的。

所以我可以按照工厂本身的第4种模式,然后在GetCustomer()中使用
锁。对?


是的,如果你愿意的话。

并不是说整个
模式中的锁定可能在那时很重要因为我是无论如何都会在另一种方法中左右锁定
Yeah but I would need the Factory to be a Singleton also or it defeats the
purpose. So I need a GetFactory() and then a GetCustomer() I guess.
Well, the factory methods themselves needn''t be part of an instance -
you don''t need to have a separate class. Any reason not just to have a
static GetCustomer method?
As to the locking... help me sort this out in my head. The problem is two
threads could call GetCustomer() right?
Yes.
Since that isn''t addressed by the
Singleton nature of the factory itself the locks are required.
Yes.
So I could follow the 4th pattern for the factory itself and then use the
locks within GetCustomer(). Right?
Yes, if you want to.
Not that the locking in the overall
pattern probably matters much at that point since I''m going to be locking
left and right in the other method anyway.




对。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Right.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于Singleton单身人士的收藏可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆