单例模式和集合 [英] Singleton Pattern and Collections

查看:75
本文介绍了单例模式和集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#天(大约6个月前)开始时,我了解了

Singleton模式并实现了参考数据,例如

出现的那种在选项对话框中。我的Singleton代码如下所示:


public sealed class参考

{

private static readonly Reference instance = new Reference() ;


//将默认构造函数设为私有,这样就无法直接创建它。

private Reference()< br $>
{

}


//只能获得此类单个实例的公共属性

public static参考数据

{

get

{

返回实例;

}

}

之后,但在课堂上,我添加了我想要的任何属性。

这对我很有帮助。


但现在我想扩展Singleton模式的使用,以包括

集合,例如Users。我的用户集合将填充一个用户

对象,该对象本身将包含许多属性,如ID,名称等。


当我将上面的代码复制到我为此目的而构建的新类时,我遇到了障碍。问题 - 我认为 - 因为父母

类已被封存,我无法再访问像User这样的子类。


所以我除了删除Singleton模式之外,我不知道如何继续。

这样可行,但并不正确,因为现在开发人员可能会错误地确定
实例化多个版本当真的应该只有一个。


我有什么想法可以解决这个问题吗?


-

Robert W.

温哥华,BC
www.mwtech.com

At the beginning of my C# days (about 6 months ago) I learned about the
Singleton pattern and implemented for Reference data, such as the kind that
appears in an Options dialog box. My Singleton code looks like this:

public sealed class Reference
{
private static readonly Reference instance = new Reference();

// Make the default constructor private, so that nothing can directly
create it.
private Reference()
{
}

// public property that can only get the single instance of this class
public static Reference Data
{
get
{
return instance;
}
}
And then after that, but within the class, I add whatever properties I want.
This has served me very well.

But now I''d like to expand the use of the Singleton pattern to include
collections such as Users. My Users collection will be populated with a User
object, that itself will contain many properties such as "ID", "Name", etc.

I hit a roadblock though when I copied the above code to a new class I had
built for this purpose. The problem - I think - is that because the parent
class is sealed I can no longer access sub-classes like "User".

So I''m not sure how to proceed, other than removing the Singleton pattern.
This works but isn''t really correct because now a developer could incorrectly
instantiate multiple versions when there really should be only one.

Any ideas how I could resolve this?

--
Robert W.
Vancouver, BC
www.mwtech.com

推荐答案

Robert W.< Ro ***** @ discussion.microsoft.com>写道:
Robert W. <Ro*****@discussions.microsoft.com> wrote:
在我的C#天(大约6个月前)开始时,我了解了
Singleton模式并实现了参考数据,例如
出现的那种选项对话框。我的Singleton代码如下所示:


< snip>


这一切似乎都是正确的。

之后,但在课堂上,我添加了我想要的任何属性。
这对我很有帮助。

但是现在我想扩展Singleton的使用模式包括诸如用户之类的集合。我的用户集合将填充一个用户
对象,该对象本身将包含许多属性,如ID,名称等。

当我遇到障碍时,我遇到了障碍将上面的代码复制到我为此目的而构建的新类中。问题 - 我认为 - 是因为父级
类是密封的,我不能再访问像用户这样的子类。

所以我不知道如何继续除了删除Singleton模式之外。
这样可行,但并不正确,因为现在开发人员可能会错误地实例化多个版本,而实际上应该只有一个版本。

我有什么想法可以解决这个问题吗?
At the beginning of my C# days (about 6 months ago) I learned about the
Singleton pattern and implemented for Reference data, such as the kind that
appears in an Options dialog box. My Singleton code looks like this:
<snip>

That all seems to be correct.

And then after that, but within the class, I add whatever properties I want.
This has served me very well.

But now I''d like to expand the use of the Singleton pattern to include
collections such as Users. My Users collection will be populated with a User
object, that itself will contain many properties such as "ID", "Name", etc.

I hit a roadblock though when I copied the above code to a new class I had
built for this purpose. The problem - I think - is that because the parent
class is sealed I can no longer access sub-classes like "User".

So I''m not sure how to proceed, other than removing the Singleton pattern.
This works but isn''t really correct because now a developer could incorrectly
instantiate multiple versions when there really should be only one.

Any ideas how I could resolve this?




我不认为我完全理解你。你是说只有

是一个用户集合吗?如果是这样,创建一个代表

用户的类,并使其成为单例。我没看到上课派生的地方

进来......


你不是从单身人士班派生出来创造另一个单身人士/>
class - 单独的类应该始终被密封(和/或只有一个

私有构造函数),否则你最终得到的东西不是
绝对单身。


-

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

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



I don''t think I quite understand you. Are you saying there should only
be one collection of users? If so, create some class representing the
users, and make that a singleton. I don''t see where class derivation
comes in...

You don''t derive from a singleton class to create another singleton
class - singleton classes should always be sealed (and/or only have a
private constructor) as otherwise you end up with something which isn''t
definitely a singleton.

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


我想我看到了罗伯特的问题。也许。


罗伯特:你不能创建一个通用的单例引用类。然后

继承它以获得不同种类的单身人士。我不会那样工作




你必须为每个班级单独实施Singleton模式

你想成为一名单身人士。这些类_can_继承自

普通父母,如果在现实世界中这样做是有意义的,但

父母不会是单身人士......它可能是抽象的,或者是设计的,只有它的子类可以实例化它。


这就是你问的问题吗?

I think I see Robert''s problem. Maybe.

Robert: you can''t create a generic "singleton reference class" and then
inherit from it to get different kinds of singletons. I doesn''t work
that way.

You have to implement the Singleton pattern separately for each class
that you want to be a Singleton. These classes _can_ inherit from a
common parent, if in real-world terms it makes sense to do so, but the
parent wouldn''t be a Singleton... it would likely be abstract, or
designed so that only its child classes could instantiate it.

Was that what you were asking?


Jon,Bruce,

感谢您的回复。你的所有见解都帮助了我。我所做的是:b $ b做错了包括嵌套类 - 例如用户,用户,

设备和设备等。在父类中。一旦我将父级

类转换为密封的Singleton类,我就无法再访问嵌套的

类了。


似乎现在这么简单,但我今天早些时候完全被难过了。


我现在的困境是,对于另一个复杂的类,我构建了一个完全的

通用XML导出函数(内置的一个从未工作过)和一个相当

的通用XML导入功能。不幸的是,当父类是单身人士时,他们似乎不会工作。但是我要调查一下

小调整是否可以解决它。


如果那不起作用那么我可以放弃Singleton -nature和只有
实例化父类一次。没错,这对于将图书馆传播给其他人来说是不正确的,但是因为我自己控制了它,所以

就足够了。


我想我现在正处于我的项目中的这一点,我想要做什么

一切都很完美和一般,我也必须得到该死的东西

完成了! :-)


-

罗伯特W.

温哥华,不列颠哥伦比亚省
www.mwtech.com


" Bruce Wood"写道:
Jon, Bruce,

Thank you both for responding. All of your insight helped me. What I was
doing wrong was including nested classes - such as "Users", "User",
"Devices", and "Device" inside the parent class. Once I turned the parent
class into a sealed Singleton class then I could no longer access the nested
classes.

Seems so simple now but I was completely stumped earlier today.

My dilemma now is that for another complex class I had built a completely
generic XML export function (the built-in one never worked) and a fairly
generic XML import function. Unfortunately they don''t appear to work when
the parent class is a Singleton one. But I''m going to investigate if a
little tweaking would resolve it.

If that doesn''t work then I can just forego the Singleton-nature and only
instantiate the parent class once. True this wouldn''t be correct for
circulating the library to others, but since I''m in control of it myself it
might suffice.

I think I''m at that point in my project now where while I want to do
everything perfectly and generically, I also have to get the damned thing
done! :-)

--
Robert W.
Vancouver, BC
www.mwtech.com

"Bruce Wood" wrote:
我想我看到了罗伯特的问题。也许。

罗伯特:你不能创建一个通用的单例引用类。然后继承它以获得不同种类的单身人士。我不会那样工作。

你必须为你想成为单身人士的每个班级分别实施单身人士模式。这些类_can_继承自一个共同的父母,如果在现实世界的术语中这样做是有意义的,但是
父母不会是一个单身人士...它可能是抽象的,或者
设计,只有它的子类可以实例化它。

那是你要问的吗?
I think I see Robert''s problem. Maybe.

Robert: you can''t create a generic "singleton reference class" and then
inherit from it to get different kinds of singletons. I doesn''t work
that way.

You have to implement the Singleton pattern separately for each class
that you want to be a Singleton. These classes _can_ inherit from a
common parent, if in real-world terms it makes sense to do so, but the
parent wouldn''t be a Singleton... it would likely be abstract, or
designed so that only its child classes could instantiate it.

Was that what you were asking?



这篇关于单例模式和集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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