Using C# 2.0 Generics to achieve a reusable Singleton pattern
2014-11-07 14:15
453 查看
一般代码:
public sealed class Singleton
{
Singleton()
{
}
public static Singleton Instance
{
get
{
return SingletonCreator.instance;
}
}
class SingletonCreator
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Singleton instance = new Singleton();
}
}用模板实现:
public class SingletonProvider <T> where T:new()
{
SingletonProvider() {}
public static T Instance
{
get { return SingletonCreator.instance; }
}
class SingletonCreator
{
static SingletonCreator() { }
internal static readonly T instance = new T();
}
}用例:
public class TestClass
{
private string _createdTimestamp;
public TestClass ()
{
_createdTimestamp = DateTime.Now.ToString();
}
public void Write()
{
Debug.WriteLine(_createdTimestamp);
}
}使用:
SingletonProvider<TestClass>.Instance.Write();
原地址:
public sealed class Singleton
{
Singleton()
{
}
public static Singleton Instance
{
get
{
return SingletonCreator.instance;
}
}
class SingletonCreator
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Singleton instance = new Singleton();
}
}用模板实现:
public class SingletonProvider <T> where T:new()
{
SingletonProvider() {}
public static T Instance
{
get { return SingletonCreator.instance; }
}
class SingletonCreator
{
static SingletonCreator() { }
internal static readonly T instance = new T();
}
}用例:
public class TestClass
{
private string _createdTimestamp;
public TestClass ()
{
_createdTimestamp = DateTime.Now.ToString();
}
public void Write()
{
Debug.WriteLine(_createdTimestamp);
}
}使用:
SingletonProvider<TestClass>.Instance.Write();
原地址:
相关文章推荐
- Introduction to Model View Control (MVC) Pattern using C#
- An Introduction to Programming Using Microsoft Visual C# 2005 Express Edition
- How To Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET
- Using ZipLib to create a Zip File in C#
- ASP.NET 2.0 C# Generics List Collection Folder Control
- How to populate the datagrid on background thread with data binding by using Visual C#
- Save Picture from Clipboard to file using C#
- [原创]C#中抓取网页方法小结 Ways to crawl web pages (get HTML) using C#
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- Proxy Pattern using C#
- A good book to learn C#2.0 ----C# 2.0 : Practical Guide for Programmers
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET(ZT)
- How To Create an ASP.NET HTTP Handler by Using Visual C# .NET
- [推荐下载]Exploring ASP.NET 2.0 Using Visual C# 2005[289221 KB]
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- [导入]Using Raw Input from C# to handle multiple keyboards
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0