(Design Pattern) Singleton.
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;
}
}
(Design Pattern) Singleton.的更多相关文章
- Design Pattern —— Singleton
Design Pattern —— Singleton 强力推荐枚举和类级内部类方式实现单例模式 单例模式是开发中非常常用的一种模式,简单的说,我们希望一个类永远都只有一个对象. 主要有两个用途: ...
- [Design Pattern] Singleton Pattern 简单案例
Singleton Pattern, 即单例模式,用于获取类的一个对象,该对象在整个应用中是其类的唯一对象.单例模式属于创建类的设计模式. SingleObject 作为单例类,内含了一个静态私有的 ...
- Design Pattern Singleton 单一模式
单一模式的几个注意点: 一) 设计单一模式,首先须要把构造函数给私有化了,不让外界訪问,那么外界仅仅能通过提供的函数获取一个新的类. 二) C++的单一模式,记得要在类外初始化一个类,否则或内存出错的 ...
- python singleton design pattern super() 多继承
python singleton design pattern decorate baseclass metaclass import module super() 一.A decorator de ...
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- 设计模式(Design Pattern)系列之.NET专题
最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- Null Object Design Pattern (Python recipe)
Null Object 个人感觉非常有用.也是在review公司其他同事写代码的时候看到. 当时使用了flask的request全局请求变量g,然后使用了g.x保存了一个东西. 当时在view代码读取 ...
随机推荐
- springmvcIntercept(拦截器)
1.创建拦截器 public class MyIntercept implements HandlerInterceptor { @Override public void afterCompleti ...
- 浮动框控制及切换、banner随机数js
1.浮动置控制及切换 <div class="banner1"></div><div class="bot_banner"> ...
- INSTALL_FAILED_INSUFFICIENT_STORAGE
现象:运行程序,进行安装时,ANDROID模拟器启动失败,在Eclipse的控制台里log显示如下错误信息 Installation error: INSTALL_FAILED_INSUFFI ...
- 超链接<a></a>
1.<a href="#" target="_self原窗口-默/_blank新窗口/_top/_parent"></a> 绝对路径:f ...
- hdu 1057 (simulation, use sentinel to avoid boudary testing, use swap trick to avoid extra copy.) 分类: hdoj 2015-06-19 11:58 25人阅读 评论(0) 收藏
use sentinel to avoid boudary testing, use swap trick to avoid extra copy. original version #include ...
- 将HTML文本框设为不可编辑文本框。
将HTML文本框设为不可编辑文本框. 方法1: onfocus=this.blur() <input type="text" name="input1" ...
- 项目中使用oracle序列
在数据库设计的时候我们可以将表的ID定义为String 然后我们可以使用序列来得到唯一的ID 手写一个mapper: <?xml version="1.0" encoding ...
- servlet jsp 客户端服务端跳转
jsp 客户端:href jsp 服务端:forward servlet 客户端:response.sendredirect(); servlet 服务器:request.getRequestDisp ...
- 2014年5月份第2周51Aspx源码发布详情
Reapter手写分页控件源码 2014-5-12 [VS2010]源码描述:实现repeater控件分页,方便好用,界面设计也很漂亮.数据库是Access,可直接运行.入口是RepeaterTes ...
- 黑马程序员——【Java基础】——GUI(图形用户界面)
---------- android培训.java培训.期待与您交流! ---------- 一.概述 1.GUI(GraphicalUser Interface):又称图形用户界面,是计算机用户与计 ...