是的,我知道... 我是个傻子。那么,什么是Singleton?[英] Yeah.. I know.. I'm a simpleton.. So what's a Singleton?

本文是小编为大家收集整理的关于是的,我知道... 我是个傻子。那么,什么是Singleton?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经尝试过几次了解单身人士.也许我太视觉了.

类似的帖子:

推荐答案

根据要求,这里有一些类比:

  • 地球
  • 宇宙
  • 元素氧(还有其他元素,但只有一个氧.
  • 真实的概念
  • false的概念

您可以实例化许多真实对象,但它们都会指代相同的实际项目(即真实的通用概念).根据您的应用程序域,可能有更多具体的示例:

  • 数据库连接
  • 应用程序的主线程
  • 任何代表硬件设备的东西(即,您只想实例化代表CPU0的对象).

其他推荐答案

单身人士是一类,您的应用程序中只能有一个实例.然后,您在整个应用程序中共享该实例.

这是一个可能会有所帮助的链接(涵盖了如何在C#中确保单身线程安全):

在C#

中实现Singleton模式

其他推荐答案

当您必须必须确保有一个和的一个一个一个实例时,单身人士很有用必须从代码中的多个位置访问对象.

如果可以一次使用您的班级的一个以上实例,那么您就不需要单身.

这是有关在哪里使用单例的一些信息: co-single.html

从前面提到的文章中:

决定班级是否真的是 辛格尔顿,你必须问自己一些 问题.

  • 每个应用程序都会以完全相同的方式使用此类? (正是关键词)
  • 每个应用程序都只需要此类的一个实例吗?
    (永远是关键词)
  • 本班的客户是否应该不知道
    的应用程序 ?

    的一部分

    如果您回答所有三个问题,那么您找到了一个 辛格尔顿.这里的要点是 如果一堂课只是单身人士 所有应用程序都准确对待 同样以及客户是否可以使用 没有应用程序上下文的课程.

本文地址:https://www.itbaoku.cn/post/627672.html

问题描述

I've tried a few times to understand what a Singleton is. Perhaps I'm just too visual.. so can anyone break it down in a simple analogy.

Similar Posts:

推荐答案

As requested, here are a few analogies:

  • The Earth
  • The Universe
  • The element oxygen (there are other elements, but only one oxygen. There are lots of oxygen molecules, but only one canonical oxygen element.)
  • The concept of True
  • The concept of False

You could instantiate lots of True objects, but they will all refer to the same actual item (i.e. the universal concept of True). Depending on your application's domain, there may be more specific examples:

  • The database connection
  • The application's main thread
  • Anything that represents a hardware device (i.e. you only want to instantiate one object representing CPU0).

其他推荐答案

A singleton is a class of which there can be only one instance in your application. You then share that instance throughout your application.

Here's a link that might help (covers how to make your singleton thread safe in c# as well):

Implementing the Singleton Pattern in C#

其他推荐答案

Singleton is useful when you must be sure that there is one and only one instance of a class, and that this object must be accessed from multiple locations in the code.

If it could make sense that more than one instance of your class could be used at once, then you don't want a singleton.

Here is some information about where to use singletons: http://www.ibm.com/developerworks/webservices/library/co-single.html

From the article mentioned previously:

To decide whether a class is truly a singleton, you must ask yourself some questions.

  • Will every application use this class exactly the same way? (exactly is the key word)
  • Will every application ever need only one instance of this class?
    (ever and one are the key words)
  • Should the clients of this class be unaware of the application they are
    part of?

    If you answer yes to all three questions, then you've found a singleton. The key points here are that a class is only a singleton if all applications treat it exactly the same and if its clients can use the class without an application context.