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

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

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

  • 相关阅读:
    Leetcode-2 两数相加
    离散数学-基本割集的找法
    Linux操作系统分析课程学习总结报告
    Linux实验三 结合中断上下文切换和进程上下文切换分析Linux内核一般执行过程
    Linux实验二:深入理解系统调用
    初始python
    水仙花数讲解
    Python-运算
    Python-列表
    Python-字符串
  • 原文地址:https://www.cnblogs.com/virusswb/p/902396.html
Copyright © 2011-2022 走看看