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

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

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

  • 相关阅读:
    使用委派代替继承
    《重构,改善既有代码的设计》读书笔记
    理解C指针: 一个内存地址对应着一个值
    C#实现窗口最小化到系统托盘
    C#中访问私有成员--反射
    不要在构造函数中调用可重写的方法
    链表解决约瑟夫环问题
    C数据结构(文件操作,随机数,排序,栈和队列,图和遍历,最小生成树,最短路径)程序例子
    java this,super简单理解
    数据与计算机通信习题
  • 原文地址:https://www.cnblogs.com/virusswb/p/902396.html
Copyright © 2011-2022 走看看