zoukankan      html  css  js  c++  java
  • C# 设计模式巩固

     前言

      设计模式的文章很多,所以此文章只是为了巩固一下自己的基础,说的不详细请见谅。

     介绍 - 单例模式

      官方定义:确保一个类只有一个实例,并提供一个全局访问点。

      通俗定义:就是一个类只有一个单个实例。(也没啥区别)

     实现

      如何保证一个类只有一个实例呢?

      1. 首先一个类是如何创建实例的? - 通过构造函数

      2. 如何保证只创建一个呢? - 构造函数只执行一次

      3. 如何保证构造函数只执行一次呢? - 访问外部类无权访问

      4. 如何让访问外部类无权访问? - 私有方法

      问题解决,上代码:

        /// <summary>
        /// 单例类
        /// </summary>
        public class Singleton
        {
            /// <summary>
            /// 私有构造函数
            /// </summary>
            private Singleton() { }
            /// <summary>
            /// 单例字段
            /// </summary>
            private static Singleton _instance; 
            /// <summary>
            /// 单例对象
            /// </summary>
            public static Singleton Instance
            {
                get
                {
                    if (_instance == null)
                        _instance = new Singleton();
                    return _instance;
                }
            }
    
            /// <summary>
            /// 输出
            /// </summary>
            public void WriteLine()
            {
                Console.WriteLine("哈希Code : " + this.GetHashCode());
            }
        }

    测试输出 HashCode :

      class Program
        {
            static void Main(string[] args)
            {
                Singleton.Instance.WriteLine();
                Singleton.Instance.WriteLine();
                Singleton.Instance.WriteLine();
                Console.Read();
            }
        }

    结果:

    这样就结束了吗? -(身为C#老鸟的我还要考虑下线程安全的问题,考虑并发的情况)

    我们需要构建一个并发的情形及多条线程同时创建对象,下面我就用简单粗暴的方法,需要对单例进行修改:

            /// <summary>
            /// 单例对象
            /// </summary>
            public static Singleton Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        Thread.Sleep(1);
                        _instance = new Singleton();
                    }
                    return _instance;
                }
            } 

    调用测试:

    class Program
        {
            static void Main(string[] args)
            {
                //线程1
                Thread thread1 = new Thread(new ThreadStart(() =>
                {
                    Singleton.Instance.WriteLine();
                }));
                //线程2
                Thread thread2 = new Thread(new ThreadStart(() =>
                {
                    Singleton.Instance.WriteLine();
                }));
                //线程3
                Thread thread3 = new Thread(new ThreadStart(() =>
                {
                    Singleton.Instance.WriteLine();
                }));
                thread1.Start();
                thread2.Start();
                thread3.Start();
                Console.Read();
            }
        }

    输出结果:

    所以我们要对代码进行改进(加锁):

    /// <summary>
        /// 单例类
        /// </summary>
        public class Singleton
        {
            /// <summary>
            ////// </summary>
            private static object locker = new object();
            /// <summary>
            /// 私有构造函数
            /// </summary>
            private Singleton() { }
            /// <summary>
            /// 单例字段
            /// </summary>
            private static Singleton _instance;
            /// <summary>
            /// 单例对象
            /// </summary>
            public static Singleton Instance
            {
                get
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            Thread.Sleep(1);
                            _instance = new Singleton();
                        }
                    }
                    return _instance;
                }
            }
    
            /// <summary>
            /// 输出
            /// </summary>
            public void WriteLine()
            {
                Console.WriteLine("哈希Code : " + this.GetHashCode());
            }
        }

    测试结果:

    解决!

    锁是个好东西但是要慎用,它是很耗资源的。所以还需要对代码进行改进,看代码我们知道只有 _instancenull 时才需要考虑并发加锁,所以我们再次改进:

        /// <summary>
        /// 单例类
        /// </summary>
        public class Singleton
        {
            /// <summary>
            ////// </summary>
            private static object locker = new object();
            /// <summary>
            /// 私有构造函数
            /// </summary>
            private Singleton() { }
            /// <summary>
            /// 单例字段
            /// </summary>
            private static Singleton _instance;
            /// <summary>
            /// 单例对象
            /// </summary>
            public static Singleton Instance
            {
                get
                {
                    if(_instance == null)
                    {
                        lock (locker)
                        {
                            if (_instance == null)
                            {
                                Thread.Sleep(1);
                                _instance = new Singleton();
                            }
                        }
                    }
                    return _instance;
                }
            }
    
            /// <summary>
            /// 输出
            /// </summary>
            public void WriteLine()
            {
                Console.WriteLine("哈希Code : " + this.GetHashCode());
            }
        }

    欢迎批评指正!转载请注明出处:http://www.cnblogs.com/xinwang/p/6294784.html 

  • 相关阅读:
    25.C++- 泛型编程之函数模板(详解)
    Windows10 + Visual Studio 2017 + CMake +OpenCV编译、开发环境配置及测试
    终于解决了python 3.x import cv2 “ImportError: DLL load failed: 找不到指定的模块” 及“pycharm关于cv2没有代码提示”的问题
    Python的开源人脸识别库:离线识别率高达99.38%(附源码)
    python获取公网ip的几种方式
    Chrome与chromedriver.exe的版本对应
    Google Gson用法
    idea 报错javax/xml/bind/DatatypeConverter
    org.slf4j:slf4j-api:添加日志管理
    基本使用——OkHttp3详细使用教程
  • 原文地址:https://www.cnblogs.com/xinwang/p/6294784.html
Copyright © 2011-2022 走看看