博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSharp设计模式读书笔记(4):单例模式(学习难度:★☆☆☆☆,使用频率:★★★★☆)...
阅读量:6947 次
发布时间:2019-06-27

本文共 3307 字,大约阅读时间需要 11 分钟。

单例模式(Singleton Pattern):确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方法。

模式角色与结构:

示例代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CSharp.DesignPattern.SingletonPattern{    class Program    {        static void Main(string[] args)        {            Singleton singleton = Singleton.Instance;            singleton.Display();            Console.ReadLine();        }    }    //// 简单实现。缺点:不是线程安全的实现方法。优点:实现了延迟初始化。    //// 注意:类定义为sealed.     //sealed class Singleton    //{    //    static Singleton _instance = null;    //    public static Singleton Instance    //    {    //        get    //        {    //            if (_instance == null)    //            {    //                _instance = new Singleton();    //            }    //            return _instance;    //        }    //    }    //    public void Display()    //    {    //        Console.WriteLine("Simple implement singleton...");    //    }    //}    //// 安全的线程。缺点:每次加锁,增加开销,损失了性能。优点:实现了延迟初始化。    //sealed class Singleton    //{    //    static Singleton _instance = null;    //    static readonly Object padlock = new Object();    //    public static Singleton Instance    //    {    //        get    //        {    //            lock (padlock)    //            {    //                if (_instance == null)    //                {    //                    _instance = new Singleton();    //                }    //                return _instance;    //            }    //        }    //    }    //    public void Display()    //    {    //        Console.WriteLine("Thread safety singleton...");    //    }    //}    //// 判定后加锁。优点:线程安全,不是每次都加锁,实现了延迟初始化。    //sealed class Singleton    //{    //    static Singleton _instance = null;    //    static readonly Object padlock = new Object();    //    public static Singleton Instance    //    {    //        get    //        {    //            if (_instance == null)    //            {    //                lock (padlock)    //                {    //                    if (_instance == null)    //                    {    //                        _instance = new Singleton();    //                    }    //                }    //            }    //            return _instance;    //        }    //    }    //    public void Display()    //    {    //        Console.WriteLine("Add lock after judgement singleton...");    //    }    //}    //// 静态初始化。缺点:不能实现延迟初始化。优点:简单常用    //sealed class Singleton    //{    //    static readonly Singleton _instance = new Singleton();    //    public static Singleton Instance    //    {    //        get    //        {    //            return _instance;    //        }    //    }    //    public void Display()    //    {    //        Console.WriteLine("Static initiate singleton...");    //    }    //}    // 延迟初始化。值得推荐。    sealed class Singleton    {        public static Singleton Instance        {            get            {                return Nested._instance;            }        }        class Nested // 嵌套类或者内部类        {            internal static readonly Singleton _instance = new Singleton();        }        public void Display()        {            Console.WriteLine("Delayed initiate singleton...");        }    }}

 

转载于:https://www.cnblogs.com/thlzhf/archive/2012/11/28/2792698.html

你可能感兴趣的文章
js获取时间戳
查看>>
Java数据结构和算法(四):栈
查看>>
为什么我的mac插入耳机耳机没有声音呢?
查看>>
ArcGIS js api 手动构建FeatureLayer
查看>>
Spark RDD持久化、广播变量和累加器
查看>>
Exception in thread "main" javax.validation.ValidationException: Unable to find a default provider
查看>>
Step by step SQL Server 2012的安装
查看>>
使用using 语句
查看>>
爱的十个秘密--5.友谊的力量
查看>>
(原創) 如何破解Quartus II 8.0 SP1? (SOC) (Quartus II) (Nios II)
查看>>
AspNetPager分页控件之url重写
查看>>
matlab练习程序(径向模糊1)
查看>>
余晟:做个懂产品的程序员
查看>>
转joson
查看>>
[翻译][erlang]cowboy路由模块使用
查看>>
HDU 2604 Queuing (矩阵乘法)
查看>>
Unity3D 记第二次面试
查看>>
[译]LINT TO SQL 介绍(数据库查询) - Part.3
查看>>
Custom Session-State Module
查看>>
QTP的那些事---有关web的自动化测试框架saffron的使用
查看>>