English 中文(简体)
利用单一州,而不是全球静态事例
原标题:Using singleton instead of a global static instance
  • 时间:2010-04-06 22:56:27
  •  标签:
  • c#
  • singleton

我今天陷入了一个问题,一位朋友建议我使用一个全球静态或更奇怪的单一州模式。 我花了几小时时间阅读单一州的情况,但一些东西仍然绕过我。

Background: What Im trying to accomplish is creating an instance of an API and use this one instance in all my classes (as opposed to making a new connection, etc).

似乎有大约100种方式创建单一州,但有一些帮助来自yoda。 我发现了一些可怕的安全例子。 .

public sealed class Singleton
{
     public static Singleton Instance { get; private set; }

     private Singleton()
     {
        APIClass api = new APIClass();  //Can this be done?
     }

     static Singleton() { Instance = new Singleton(); }
}

你们怎么会立即介绍这一新类别,以及如何把它从一个单独类别中提出来?

EDIT: I realize the Singleton class can be called with something like

Singleton obj1 = Singleton.Instance();

但是,我是否能够在APICES类(例如, ob1.Start)中查阅这些方法? (我不需要,只是要求)

EDIT #2:在检查答案方面,我可能已经是很早的,但我确实有一件小事,仍然给我造成问题。 公益物协会只是罚款,不幸的是,它能够发动两次事件?

新法

public sealed class SingletonAPI
{
 public static SingletonAPI Instance { get; private set; }

 private SingletonAPI() {}

 static SingletonAPI() { Instance = new SingletonAPI(); }     

 // API method:
 public void Start() { API myAPI = new API();}
 }

但是,如果我试图这样做的话......

SingletonAPI api = SingletonAPI.Instance;
api.Start();
SingletonAPI api2 = SingletonAPI.Instance;  // This was just for testing.
api2.Start();

我有一个错误,说我不能开一个以上的例子。

最佳回答

为什么不仅仅在你独一无二的地方增加公共财产?

public sealed class Singleton
{
     public static Singleton Instance { get; private set; }

     private APIClass _APIClass; 

     private Singleton()
     {
        _APIClass = new APIClass();  
     }

     public APIClass API { get { return _APIClass; } }

     static Singleton() { Instance = new Singleton(); }     
}

接着,你打电话的网址是:

Singleton.Instance.API.DoSomething();

或者,如果你是APIC集团的作者,你可以把它变成一个单一州,而不是把它放在一个单一州:

public sealed class SingletonAPI
{
     public static SingletonAPI Instance { get; private set; }

     private SingletonAPI() {}

     static SingletonAPI() { Instance = new SingletonAPI(); }     

     // API method:
     public void DoSomething() { Console.WriteLine("hi"); }
}

内容提要:

SingletonAPI.Instance.DoSomething();
问题回答

你们会立即放下这门课,这是你第一次使用这个班子。 你使用的方法的优点是,它已经变得安全(无论有多少条路面试图进入它), la(在你试图进入单一州级之前,它赢得了瞬时的电击),而且在执行过程中很简单。

你们都需要这样做:

 Singleton.Instance.MyMethodOnSingleton();

或:

 Singleton myInstance = Singleton.Instance; // Will always be the same instance...
 myInstance.DoSomething();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签