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

推荐订阅源

V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Latest
Security Latest
H
Hacker News: Front Page
T
Tenable Blog
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Project Zero
Project Zero
O
OpenAI News
AI
AI
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
NISL@THU
NISL@THU
A
Arctic Wolf
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
U
Unit 42
S
Security Affairs
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
月光博客
月光博客
Engineering at Meta
Engineering at Meta
腾讯CDC
F
Full Disclosure
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
The Cloudflare Blog

博客园 - jeky

rails 路由配置时 URL 地址如何匹配下划线? github.com 不能访问怎么办? Linux - 显示日历的命令 cal 用法 Linux - echo 命令如何追加文本? 如何解决ssh连接后长时间不操作断线的问题? Rails 中如何使用全局变量? raw 允许输出html字符 Linux 用户及组 话说调试 全等(===) 委托的协变与逆变 判断一个整数是奇数还是偶数 - jeky - 博客园 选择排序法 用Javascript实现关键词的高亮显示 Javascript中正则表达式的相识知识 对 TextBox 设置css属性 注意void()里面不能空 - jeky - 博客园 OWC11 实例 访问数组元素的 3 种方法 Internet Explorer 7.0(IE7.0)简体中文版
让我笔试吃亏"单例模式"
jeky · 2009-03-25 · via 博客园 - jeky

让我笔试吃亏的题目:单例模式。

因为是纯英文试卷,而且我从来没有用过单例模式。

答题时,有点儿懵了,竟然在类的实例构造函数里瞎写一番。

回来之后,终于静下来心,踏踏实实地学习了一下。记录在此。

1. 单例的目的是什么?  
    这个应该很明显,保证一个类只有单一的实例,也就是说你无法通过New或CreateInstance来创建这个类的一个新实例。    
    
2. 单例的好处在哪里?
    当一个对象在程序内部只能有一个实例的时候,它可以保证我们不会重复创建,而是始终指向同一个对象。    

3. 应用场景:

    如(记录系统日志.log)

4. 参考文章:
    《Implementing the Singleton Pattern in C#》
    http://www.yoda.arachsys.com/csharp/singleton.html

simple thread-safety

    // 密封类,不允许派生子类
    public sealed class Singleton
    {
        
static Singleton instance = null// 静态实例
        static readonly object padlock = new object(); // 静态只读锁

        Singleton() { } 
// 私有的构造函数// 公共的静态属性:获取类的实例
        public static Singleton Instance
        {
            
get
            {
                
lock (padlock)
                {
                    
if (instance == null)
                        instance 
= new Singleton();
                }
                
return instance;
            }
        }
    }

not quite as lazy,

but thread-safe without using locks

    // 密封类,不允许派生子类
    public sealed class Singleton
    {
        
static readonly Singleton instance = new Singleton(); // 静态只读实例

        
static Singleton() { } // 静态构造函数,用于是初始化 instance 变量

        Singleton() { } 
// 实例构造函数// 公共的静态属性:获取类的实例
        public static Singleton Instance
        {
            
get
            {
                
return instance;
            }
        }
    }