ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

单例模式的三种写法

2021-10-24 09:00:34  阅读:162  来源: 互联网

标签:singleton System singletonstatic 三种 单例 using static 写法 public


    单例模式是23设计中创建模式的第一个,也是入门的设计模式,好多net 程序员 工作7-8 你让他写3-4个设计模式,我敢说,很多人可能都写不出来,因为net 封装的太厉害了,很多程序员有种惰性,总是"约定俗成" 我就这么用的。C# 语言是一款优秀的语言,并不比java差,只是java 开源早,生态环境好。好了不废话了。直接上三中写法。

   经典写法,双if +lock ,第一个if 判断是为了节省资源,第二个判断是否已经创建对象。。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesgersClass
{
    public class singleton
    {
        private static object _lock=new object();  
        private static  singleton singletons;

        public int m = 0;
        public int add()
        { 
         return m++;
        }
        private singleton()
        {
                
        }
        public static singleton CreateIntance()
        {
            if (singletons == null)
            {
                lock (_lock)//双if +lock
                {
                    if (singletons == null)
                    {
                        singletons = new singleton();
                    }
                }
            }
            return singletons;
        }

    }
}

 

客户端调用时 我们不难看出虽然我们循环100次 但是 全局就一个对象,所以a是累加的。

using DesgersClass;
using System;

namespace Desgionser23
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            singleton singleton= singleton.CreateIntance();
            var a = 0;
            for (int i = 0; i < 100; i++) {
             a=   singleton.add();
                Console.WriteLine($"*******************************************");
                Console.WriteLine($"a={a}");
            }

            Console.WriteLine();
        }
    }
}

 

 

 

 

 

 

 

第二种和第三种 基本套路是一样的,都是利用底层clr 保证静态字段和静态构造函数只创建一次

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesgersClass
{
    public class singletonstaticProp
    {

        private static singletonstaticProp singletonstaticProps = new singletonstaticProp();
        public static singletonstaticProp CreateInstance()
        {
            return singletonstaticProps;
        }

    }
}

 

*********************

第三种

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesgersClass
{
    public class singletonstatic
    {
        private static singletonstatic singletonstatics;

        static singletonstatic()
        {
            singletonstatics = new singletonstatic();
        }

        public static singletonstatic CreateInstance()
        {
            return singletonstatics;
        }

    }
}

 

标签:singleton,System,singletonstatic,三种,单例,using,static,写法,public
来源: https://www.cnblogs.com/jasontarry/p/15450252.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有