惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 神秘狭

成熟男人必看的5个故事 很有感觉的爱 - 神秘狭 在新电脑上部署网站发现的问题(asp.net) 十句话 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 问题 - 神秘狭 EXCEL下载功能在XP系统中运行是好好的,到windows2003系统上,就报错 ASP.NET设置数据格式与String.Format和DataFormatString使用总结 - 神秘狭 VS2005打开js文件,中文都是乱码 - 神秘狭 回发或回调参数无效。在配置中使用 问题解放方法 - 神秘狭 - 博客园 编译时DEBUG中dll文件删除问题 - 神秘狭 javascript日期比较 asp.net获取URL和IP地址 (转) QQ登不上去,提示不能访问QQ服务器端口---解决办法 - 神秘狭 很有感觉的两首歌 - 神秘狭 keyAscii码表 自制花肥小妙招(转) 让网页上的图片都动起来 黄石火车票信息 @键和"键互换问题
设计模式学习(一) 单件模式
神秘狭 · 2007-11-29 · via 博客园 - 神秘狭

一、单件模式的意图
保证一个类仅有一个实例,并提供一个访问它的全局访问点。

二、应用场景

l         每台计算机可以有若干个打印机,但只能有一个Printer Spooler,避免两个打印作业同时输出到打印机。
(摘自吕震宇的C#设计模式(7)-Singleton Pattern

l         PC机中可能有几个串口,但只能有一个COM1口的实例。

l         系统中只能有一个窗口管理器。

l         .NET Remoting中服务器激活对象中的Sigleton对象,确保所有的客户程序的请求都只有一个实例来处理。

三、实例

 1 1public sealed class Spooler 
 2 2{
 3 3    static Spooler instance=null;
 4 4
 5 5    private Spooler()
 6 6    {
 7 7    }

 8 8
 9 9    public static Spooler Instance
1010    {
1111        get
1212        {
1313            if (instance==null)
1414            {
1515                instance = new Spooler();
1616            }

1717            return instance;
1818        }

1919    }

2020}

1sealed class Singleton
2{
3   private Singleton();
4   public static readonly Singleton Instance=new Singleton();
5}

以上两个实例产生的效果相同.但存在多线程时的并发性,安全性, 无法实现延迟初始化等问题.可以查看一下两个网页来了解更多单件模式的知识.

http://terrylee.cnblogs.com/archive/2005/12/09/293509.html

http://www.cnblogs.com/birdshover/archive/2006/06/15/424656.aspx