(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代码读取 ...
随机推荐
- Java数据库操作
一.JDBC 1.JDBC Java数据库连接,用于Java程序中实现数据库操作功能,java.sql包中提供了执行SQL语句,访问各种数据库的方法,并为各种不同的数据库提供统一的操作接口及类. 2. ...
- css小知识点
1.div如何让背景是透明的 如:<div class="ha"></div> <style> .ha{ background:transpar ...
- jquery 失去焦点时输入框为空时自动填写默认内容
$("#address").focus(function () { // 地址框获得鼠标焦点 var txt_value = $(this).val(); // 得到当前文本框的值 ...
- JS转换时间戳为“刚刚”、“1分钟前”、“2小时前”“1天前”等格式
var minute = 1000 * 60; var hour = minute *60; var day = hour *24; var week = day * 7; var month = d ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- POJ 3694 tarjan 桥+lca
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7298 Accepted: 2651 Descripti ...
- Node.js 函数
Node.js 函数 在JavaScript中,一个函数可以作为另一个函数接收一个参数.我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数. Node.js中函数的使用与Javasc ...
- MyEclipse中Maven的配置
之前在MyEclipse这个IDE中配置Maven,完成配置后启动Maven时出现-Dmaven.multiModuleProjectDirectory system propery is not s ...
- MATLAB 图像分类 Image Category Classification Using Bag of Features
使用MATLAB实现图像的识别,这是MATLAB官网上面的例子,学习一下. http://cn.mathworks.com/help/vision/examples/image-category-cl ...
- LVDS,MIPI,EDP
一.背景介绍: 随着显示分辨率的越来越高,传统的VGA.DVI等接口逐渐不能满足人们的视觉需求.随后就产生了以HDMI.DisplayPort为代表的新型数字接口,外部接口方面HDMI占据了较大市场优 ...