C#:具有继承的Singleton模式? [英] C#: Singleton Pattern with inheritance?

查看:57
本文介绍了C#:具有继承的Singleton模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下是否可行:


我想创建一个抽象的Singleton类,它实现了具有限制的单例

行为,在getInstance()方法中不会创建唯一对象



从Singleton类派生的类必须实现一个

静态构造函数,它们将实例加载到静态唯一的

变量中。


现在我不得不遇到问题:


1.是否可以在自己的静态

构造函数中创建类的实例?

2.静态唯一变量位于基类,所以对于所有派生类来说,它将是

相同,而整个想法只是愚蠢的。或者

每个派生类都有自己的静态唯一吗?


非常感谢...


踩踏

I woundered if the following would be possible:

I want to create an abstract Singleton class, which implements the singleton
behaviour with the limitation, that the unique object will not be created
within the getInstance() method.
The classes which are derived from the Singleton class, have to implement a
static constructor, where they load an instance into the static unique
variable.

Now I have to problems with that:

1. Is it possible to create an instance of a class inside its own static
constructor?
2. The static unique variable is inside the base class, so it would be the
same for all the derived classes and the whole idea is just stupid. Or has
every derived class its own static unique?

thanks a lot...

Stampede

推荐答案

踩踏事件,


不是说你不受欢迎而不是你得不到答案<这里是



FYI:InterNet上最活跃的开发者新闻组之一是

Microsoft.public.dotnet.languages.csharp


在那个新闻组中,有更多的人可以并且将会回答你的C#

问题,并回顾其他人的答案。


我希望这会有所帮助,


Cor
Stampede,

Not that you are not welcome here and not that you cannot get an answer
here.

FYI: one of the most active developers newsgroups on InterNet is
Microsoft.public.dotnet.languages.csharp

In that newsgroup are much more people who can and will answer your C#
questions and review the answers from others.

I hope this helps,

Cor


大约需要5行代码才能成为一个单身人士所以我认为你

应该把它们写出来。


但更重要的是,如果你想制作一个表格单身,你需要

制作SingletonForm。如果你想让MyClass成为一个单独的

你需要SingletonMyObject等等,所以你只需要获得专业化的扩展。


只需我的2美分,

Jan

It''s about 5 lines to make somthing into a Singleton so I think you
should just write them out.

But more importantly, if you wanted to make a Form a Singleton you need
to make SingletonForm. If you wanted to make MyClass into a singleton
you''d need SingletonMyObject and so forth so you''d just get a
proliferation of specializations.

Just my 2 cents,
Jan


我很抱歉打扰你的新闻组与我的愚蠢,错误的

问题。我将移动到你告诉我的新闻组,我搜索过,但

找不到,并希望那里的人会更友好,

因为我总是尽量保持友好,即使有人做了一些愚蠢的事情,

新人会发生什么事情,就像我一样。


无论如何,谢谢你告诉我正确的新闻组,对不起我的坏话

英语,这可能会打扰你。


问候


踩踏事件


" Cor Ligthert [MVP]"写道:
I''m sorry for interrupting your newsgroup with my stupid, wrong-placed
question. I will move to the newsgroup you told me, which I searched for, but
coudn''t find, and hope that the people there will be a little more friendly,
because I always try to stay friendly, even if someone does something stupid,
what mostly happens with new people, as I am one.

Anyway, thanks for telling me the right newsgroup and sorry for my bad
English, which may have disturbed you too.

greetings

Stampede

"Cor Ligthert [MVP]" wrote:
踩踏事件,

并不是说你不欢迎在这里而不是你在这里得不到答案
<仅供参考:InterNet上最活跃的开发者新闻组之一是
Microsoft.public.dotnet.languages.csharp

在那个新闻组中,有更多的人能够并且将会回答你的C#
问题并回顾其他人的答案。

我希望这会有所帮助,

Stampede,

Not that you are not welcome here and not that you cannot get an answer
here.

FYI: one of the most active developers newsgroups on InterNet is
Microsoft.public.dotnet.languages.csharp

In that newsgroup are much more people who can and will answer your C#
questions and review the answers from others.

I hope this helps,

Cor



这篇关于C#:具有继承的Singleton模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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