zoukankan      html  css  js  c++  java
  • CSharp设计模式读书笔记(4):单例模式(学习难度:★☆☆☆☆,使用频率:★★★★☆)

    单例模式(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...");
            }
        }
    }
  • 相关阅读:
    cv2.SIFT() AttributeError: 'module' object has no attribute 'SIFT' OpenCV Python can't use SURF, SIFT
    CMake Error at cmake/OpenCVUtils.cmake
    安装opencv3.x卡在ICV: Downloading ippicv_linux_20151201.tgz...
    CMake Error at cmake/OpenCVModule.cmake:295 (message): No extra modules found in folder:Please provide path to 'opencv_contrib/modules' folder
    关于池化(pooling)理解!!!
    使用qt creator4.XXX,b编辑和调试caffe,太好用了
    Ubuntu下好的PDF阅读器介绍
    Qt的action控件中采用默认绑定,没有connect显示绑定!!!
    Ubuntu中更改所有子文件和子目录所有者权限
    关于ubuntu14.04,忘记root密码解决方案(经测试,内核3.13和4.2可行)。
  • 原文地址:https://www.cnblogs.com/thlzhf/p/2792698.html
Copyright © 2011-2022 走看看