Role:

The purpose of the Singleton pattern is to ensure that there is only one instance of a class, and that there is a global access point to that object.

Design:

  • Make the constructor private and add a private static constructor as well.
  • Add a private static read-only object that is internally instantialed using the private constructor.
  • Add a public static property that accesses the private object.

Implementation:

    public sealed class Singleton
{
// Private constructor.
private Singleton()
{
Console.WriteLine("One instance is created.");
} // private object instantiated with private constructor.
private static readonly Singleton instance = new Singleton(); // Public static property to get the object.
public static Singleton UniqueInstance
{
get
{
return instance;
}
}
}
    class Program
{
static void Main(string[] args)
{
Singleton s0 = Singleton.UniqueInstance;
Singleton s1 = Singleton.UniqueInstance;
Singleton s2 = Singleton.UniqueInstance;
}
}

最新文章

  1. 解决VS调试时断点不会命中
  2. offsetTop、clientTop、scrollTop、offsetTop属性
  3. h5 hdf5 文件转 tif 流程
  4. 很全的corel图像分类,场景识别图像库
  5. css的框架——global.css
  6. 如何调试什么时候SaveFileDialog会被Dispose
  7. Linux PATH变量的设置
  8. 单例-b
  9. 记一个SwipeMenuListView侧滑删除错乱的Bug
  10. Xapian索引-文档检索过程分析之匹配百分比
  11. Nginx禁止IP直接访问网站
  12. JavaScript(一)
  13. Mysql中NUMERIC和DECIMAL类型区别比较
  14. Dom4j基础
  15. webstorm 格式化代码及常用快捷键
  16. 使用mod_deflate模块压缩页面优化传输速度
  17. openLayer3地图的使用心得
  18. 【译】AI 让科技公司变得更强大吗
  19. 02 Tensorflow的安装配置
  20. ASP.NET网站性能优化

热门文章

  1. 第一个Android程序
  2. eclipse 本地项目提交到远程库以及从远程库中添加项目 ---git
  3. guava学习--Supplier Suppliers
  4. Svn常见问题及相关原因
  5. Apache+Tomcat构建Tomcat负载均衡集群
  6. NSAssert的使用
  7. struts2 标签 和 c标签的页面数据显示
  8. 删除oracle表中的完全重复数据
  9. 查找SAP标准程序用户出口及BADI的方法
  10. PTA作业