zoukankan      html  css  js  c++  java
  • 单例模式

    1.懒汉模式

    class NormalCase
         {
            public string Name { get; set; }
    
            //第一步:构造函数私有化: 外界不能用new 进行实例
            private NormalCase() { }
    
            //第二步:自行创建一个空实例 static代表只能创建一个实例
            private static NormalCase normalCase=null;//赖汉模式
    
            //第三步:提供外界可访问实例的方法
            public static NormalCase getInstance()
            {
                //存在安全:多线程并发:会产生多个实例
                if (normalCase==null)
                {
                    normalCase = new NormalCase();
                }
                return normalCase;
            }
    
            public void ShowMsg()
            {
                Console.WriteLine(Name);
            }
        }

    2.普通类改为单利模式

    class NormalCase
        {
            public string Name { get; set; }
    
            //第一步:构造函数私有化: 外界不能用new 进行实例
            private NormalCase() { }
    
            //第二步:自行创建一个实例 static代表只能创建一个实例
            private static NormalCase normalCase = new NormalCase();//饿汉模式
    
            //第三步:提供外界可访问实例的方法
            public static NormalCase getInstance()
            {
                return normalCase;
            }
    
            public void ShowMsg()
            {
                Console.WriteLine(Name);
            }
        }

    3.内部静态类实现双重验证

    class NormalCase
        {
            public string Name { get; set; }
    
            //第一步:构造函数私有化: 外界不能用new 进行实例
            private NormalCase() { }
    
            //第二步:内部静态类
            private static class NormalCaseHelper
            {
                public static NormalCase normalCase = new NormalCase();
            }
    
            //第三步:提供外界可访问实例的方法
            public static NormalCase getInstance()
            {
                return NormalCaseHelper.normalCase;
            }
    
            public void ShowMsg()
            {
                Console.WriteLine(Name);
            }
        }

    4.双重验证

    class NormalCase
        {
            //定义一个锁资源
            private static readonly Object obj = new object();
            public string Name { get; set; }
            //第一步:构造函数私有化: 外界不能用new 进行实例
            private NormalCase() { }
    
            //第二步:自行创建一个空实例 static代表只能创建一个实例
            private static NormalCase normalCase=null;//赖汉模式
    
            //第三步:提供外界可访问实例的方法
            public static NormalCase getInstance()
            {
                //一重验证
                if (normalCase==null)
                {
                    lock(obj)
                    {
                        //二重验证
                        if (normalCase == null)
                        {
                            normalCase = new NormalCase();
                        }
                    }              
                }
                return normalCase;
            }
            public void ShowMsg()
            {
                Console.WriteLine(Name);
            }
        }

    5.控制台输出

    class Program
        {
            static void Main(string[] args)
            {
                NormalCase nc = NormalCase.getInstance();
                nc.Name = "Jack";
                nc.ShowMsg();
    
                NormalCase nc1 = NormalCase.getInstance();
                nc1.ShowMsg();
    
                Console.Read();
            }
        }
  • 相关阅读:
    【SignalR学习系列】3. SignalR实时高刷新率程序
    【SignalR学习系列】4. SignalR广播程序
    【SignalR学习系列】5. SignalR WPF程序
    python gb2312 转换为 utf-8
    爬虫 需要什么样的 CPU,内存 和带宽
    TypeError: sequence item 0: expected string, Tag found
    MySQL 数据的 截取,数据清洗
    MySQL (1366, "Incorrect string value: '\xF0\x9F\x8E\xAC\xE5\x89...' for column 'description' at row 1")
    微博爬虫 ----- 微博发布时间清洗
    ReferenceError: weakly-referenced object no longer exists Python kafka
  • 原文地址:https://www.cnblogs.com/lhl123/p/10637433.html
Copyright © 2011-2022 走看看