zoukankan      html  css  js  c++  java
  • [原创]singleton,design pattern Virus

    thank you for your read
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
        //single thread
        public class Singleton
        {
            private static Singleton instance;

            private Singleton() { }

            public static Singleton Instance
            {
                get
                {
                    if (instance == null)
                        return new Singleton();
                    return instance;
                }
            }
        }

        //multiple thread
        public class SingletonTest
        {
            private static volatile SingletonTest instance;
            private static object lockHelper = new object();

            private SingletonTest() { }

            public static SingletonTest Instance
            {
                get
                {
                    if (instance == null)
                    {
                        lock (lockHelper)
                        {
                            if (instance == null)
                                return new SingletonTest();
                        }
                    }
                    return instance;
                }
            }
        }

        //simple singleton in vary environment
        public class SimpleSingleton
        {
            //inline instance
            public static readonly SimpleSingleton Instance = new SimpleSingleton();

            private SimpleSingleton() { }
        }

         //be equal to above
        public class singleton
        {
            public static readonly singleton instance;

            //can not have parameter, in multithread environment it can execute only once
            static singleton()
            {
                instance=new singleton();
            }

            private singleton() { }
        }

        class Program
        {
            static void Main(string[] args)
            {
               
               
            }
        }
    }

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    github提交忽略idea
    快速上手 Python 命令行模块 Click
    Clean Python第四章元类部分 4-15演示代码修正
    Python Type Hint类型注解
    docker运行python3.8
    python3 aes加解密代码(PCKS7,CBC模式,Base64结果)
    从源代码分析Universal-Image-Loader中的线程池
    从源代码分析Android-Universal-Image-Loader的缓存处理机制
    从源代码分析Android-Universal-Image-Loader图片下载技巧
    【译】UNIVERSAL IMAGE LOADER.PART 2---ImageLoaderConfiguration详解
  • 原文地址:https://www.cnblogs.com/virusswb/p/902396.html
Copyright © 2011-2022 走看看