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

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

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

  • 相关阅读:
    Junit单元测试学习笔记(一)
    perl 函数参数传递与返回值(一)
    Oracle blob字段类型 文件读写实例
    测试沙龙的一些感悟
    常用排序算法选择排序
    perl 哈希(hash)学习笔记(一)
    perl 自定义包/模块的使用(正则表达式匹配email地址和ip)
    常用排序算法冒泡排序
    如何使用excel计算工龄
    畅想(3)打通编程的任督二脉 人工智能
  • 原文地址:https://www.cnblogs.com/virusswb/p/902396.html
Copyright © 2011-2022 走看看