首页 > 其他分享 >Singleton Design Parttern

Singleton Design Parttern

时间:2023-03-04 09:44:29浏览次数:35  
标签:线程 Singleton private instance Design Parttern new static public

单例模式是最常见也是最简单的设计模式,保证一个类只有一个实例并且提供一个全局访问点,主要解决实例被频繁的创建和销毁可能带来内存消耗问题。

单例模式的特点:

   1、私有无参构造函数

    2、私有静态变量存储单个实例创建的引用

    3、共有静态的全局访问点(可以是方式也可以是属性)

以下介绍多种实现方式:

1、线程不安全的单例

    The multiple threads could have evaluated the test if (_intance==null) and fount it to be true, then multiple instances will be created, which violates the singleton parttern.

    public sealed class SingletonPartternDemoV1
    {
        private static SingletonPartternDemoV1? _instance;
        private SingletonPartternDemoV1() { }

        public static SingletonPartternDemoV1 Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new SingletonPartternDemoV1();
                return _instance;
            }
        }
    }
thread-unsafe

2、线程安全单例

    创建实例时加锁(lock),在同一个时刻只允许一个线程创建实例,带来的问题是效率,多个线程获取实例需要等待。

public sealed class SingletonPartternDemoV2
    {
        private static readonly object _obj = new object();
        private static SingletonPartternDemoV2? _instance;
        private SingletonPartternDemoV2() { }

        public static SingletonPartternDemoV2 Instance
        {
            get
            {
                lock (_obj)
                    if (_instance == null)
                        _instance = new SingletonPartternDemoV2();
                return _instance;
            }
        }
    }
thread-safe

3、线程安全双重检测

   可以检测多个线程获取实例等待的问题

public sealed class SingletonPartternDemoV3
    {
        private static readonly object _obj = new object();
        private static SingletonPartternDemoV3? _instance;
        private SingletonPartternDemoV3() { }

        public static SingletonPartternDemoV3 Instance
        {
            get
            {
                if (_instance == null)
                    lock (_obj)
                        if (_instance == null)
                            _instance = new SingletonPartternDemoV3();
                return _instance;
            }
        }
    }
thread-safe double checking

4、线程安全没有锁

   通过私有静态属性初始化实例

public sealed class SingletonDemoV4
    {
        private static readonly SingletonDemoV4 instance = new SingletonDemoV4();
        static SingletonDemoV4() { }
        private SingletonDemoV4() { }
        public static SingletonDemoV4 Instance
        {
            get
            {
                return instance;
            }
        }
    }
Thread Safe Singleton without using locks and no lazy instantiation

5、System.Lazy

/// <summary>
    /// 1 Using .Net4 or higher then you can use the System.Lazy<T> type to make the laziness really simple
    /// 2 You can pass a delegate to the constructor that calls the singleton constructor, which is done most easily with a lambda expression.
    /// 3 Allow you to check whether or not the instance has beed created with the IsValueCreated property.
    /// </summary>
    public sealed class SingletonDemoV5
    {
        private SingletonDemoV5() { }
        private static readonly Lazy<SingletonDemoV5> instance = new Lazy<SingletonDemoV5>(() => new SingletonDemoV5(), true);
        public static SingletonDemoV5 Instance
        {
            get
            {
                return instance.IsValueCreated ? instance.Value : new SingletonDemoV5();
            }
        }
    }
System.Lazy

 

标签:线程,Singleton,private,instance,Design,Parttern,new,static,public
From: https://www.cnblogs.com/qindy/p/17176577.html

相关文章

  • 在线设计器 DesignO 的分析
    需求分析现有POD网站的在线编辑器不是很好用。可配置性不强,素材无法在后台实现管理可扩展性不强,无法应用于多个行业,比如包装、服装产品分析官方网站:https://www.de......
  • PowerDesigner显示Comment注释
    PowerDesigner默认显示的列是Name及类型,如下图示 现在需要显示注释列,以便使得ER图更加清晰。但是PowerDesigner勾选Comment显示没有效果,所以通过以下几步来处理:双击表......
  • powerdesigner 导入sql
    将.sql文件导入powerdesigner的步骤是本文我们主要要介绍的内容,步骤如下:第一步:将要导入的库的所有表的表结构(不要表数据,只要表结构)导出成一个.sql文件。第二步:在powerdes......
  • webtest / testcase design / pandingbiao
    s一,判定表法的定义二,为什么要使用判定表法三,判定表法的优缺点1,优点2,缺点四,判定表法的四大组成部分五,判定表的规则与合并标准规则:六,判定表法的适用场景七,判定表法分析案例......
  • 全志T3+FPGA国产核心板——Pango Design Suite的FPGA程序加载固化
    本文主要基于紫光同创PangoDesignSuite(PDS)开发软件,演示FPGA程序的加载、固化,以及程序编译等方法。适用的开发环境为Windows7/1064bit。测试板卡为全志T3+LogosFPGA......
  • 软件测试与检验复习1(Introduction to Software Engineering/Software Testing/Test-Dr
     WhatisSoftwareEngineering?什么是软件工程Theprocessofsolvingcustomers'problemsbythesystematicdevelopmentandevolutionoflarge,high-quali......
  • PyQt6,QTDesigner安装
    安装PyQt6和pyqt6-toolspipinstallPyQt6pipinstallpyqt6-tools在pycharm中添加外部工具QTDesigner和PYUIC设置路径: 点+,创建新的工具  program填pyqt6......
  • AntDesign日期范围选择(可复用代码片段)
    <template><div><a-range-pickerformat='YYYY-MM-DD'@change='onUpdateChange':default=value='[dataParams.updatedTime_begin,dataParams.updateTi......
  • python -- PyQt5(designer)安装详细教程
    摘自:https://blog.csdn.net/weixin_64338372/article/details/128111818先展示一下安装好后的效果如下:  PyQt5基本教程大全​​​​​​​http://t.csdn.cn/L50jl......
  • formdesigner支持jeecgboot的部门与人员组件
      根据网友tom提供的代码,集成了formdesigner支持jeecgboot的部门与人员组件。1、人员组件在formdesigner组件的items下建立userList.js/***用户组件*/exportletus......