嵌套单例类

发布于 2024-08-15 09:50:32 字数 661 浏览 4 评论 0原文

C# 中是否可以将单例类嵌套在非单例类中?如果可以,在这种情况下单例的生命周期是否有任何限制?

public class NonSingletonClass
{
  public NonSingletonClass()
  {
    // Initialize some stuff.
  }

  // Put some methods here.

  public class SingletonClass
  {
    // Singleton construction goes here.

    // Put some methods here.
  }
}

在我的应用程序的设计中,这是有道理的,但在实际实现之前我需要知道任何潜在的问题。

编辑:具体来说,我有一个基于计时器执行任务的作业主机。本例中的 NonSingletonClass 是任务的实例化。 SingletonClass 是由 NonSingletonClass 使用的数据存储库,但可以从 NonSingletonClass 外部进行修改。 SingletonClass 的存在是为了确保即使有多个 NonSingletonClass 实例,所有这些实例仍然只有一组指令和数据。

Is it possible to nest a singleton class inside a non-singleton class in C#, and if so, are there any restrictions to the life-cycle of the singleton in this case?

public class NonSingletonClass
{
  public NonSingletonClass()
  {
    // Initialize some stuff.
  }

  // Put some methods here.

  public class SingletonClass
  {
    // Singleton construction goes here.

    // Put some methods here.
  }
}

In the design of my application, this makes sense, but I need to know of any potential gotchas before I actually implement this.

EDIT: Specifically, I have a job host that executes tasks based on a timer. The NonSingletonClass in this case is an instantiation of a task. The SingletonClass is a repository of data that is used by NonSingletonClass, but can be modified from outside of NonSingletonClass. SingletonClass exists to ensure that even if there are multiple NonSingletonClass instances, there will still be only one set of instructions and data for all of them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不美如何 2024-08-22 09:50:32

是的,单身人士可以在非单身人士中完全舒适地生活。

嵌套类的生命周期完全独立于外部类的生命周期。 C# 中的嵌套类和非嵌套类之间几乎没有什么区别。 (存在一些差异 - 例如,嵌套类可以访问外部类的私有成员,并且扩展方法必须位于非嵌套静态类中 - 但这不会影响 ?

您有具体的担忧吗

Yes, a singleton can live entirely comfortably within a non-singleton.

The lifecycle of the nested class is entirely independent on the lifecycle of the outer class. There's very little difference between a nested class and a non-nested class in C#. (There are some differences - the nested class has access to private members of the outer class, and extension methods have to be in a non-nested static class, for example - but it doesn't affect the lifecycle.

Did you have a specific concern?

孤芳又自赏 2024-08-22 09:50:32

我相信您正在寻找单态模式,

此链接对此进行了很好的描述,并提供了单例模式的合同
单态模式

I believe you are looking for a monostate pattern

this link is a good description of this and offers contrats with a singleton pattern
monostate pattern

浪推晚风 2024-08-22 09:50:32

我想我的第一个问题是,考虑到您所说的“我的应用程序的设计是有意义的”,您能否给我们提供更多细节。抽象工厂和构建器只是使用单例类的一些模式。您是否存储某种要在 NonSingletonClass 中使用的全局数据?

I guess my first question is could you give us more specifics considering you stated "the the design of my application this makes sense". Abstract factory and builder are just some patters that use the singleton classes. Are you storing some sort of global data to be used within NonSingletonClass.

冰魂雪魄 2024-08-22 09:50:32

考虑到两个类都没有对另一个类的引用,我认为嵌套单例不会有任何问题。考虑到您说单例为多个实例提供可变数据存储,我认为您的潜在问题将与并发相关。

这些仅在允许多线程访问的情况下适用,也就是说,首先我们必须确保适当的锁到位,以便单例始终处于有效状态。其次,如果单例状态正在改变并且对象以非确定性顺序访问,则结果将不可预测(即竞争条件)。

如果并发不是应用程序的问题,我发现使用私有嵌套类是隐藏实现细节并确保每个对象只有一个角色的绝佳方法。

I don't think that there are any problems with a nested singleton considering that neither class holds a reference to the other. Considering that you say that the singleton provides mutable data storage for several instances, I think your potential problems are going to be concurrency related.

These apply only if multi-thread access is allowed, that is, first we have to ensure the appropriate locks are in place so that the singleton is always in a valid state. And second, if the singleton state is changing and objects are accessing in non-deterministic order, the results will not be predictable (i.e. a race condition).

If concurrency is not an issue with the application, I find the use of private nested classes to be an excellent way of hiding implementation details and ensuring each object has only one role.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文