实现Singleton模式-C#

// 面试题2:实现Singleton模式
// 题目:设计一个类,我们只能生成该类的一个实例。

using System;

namespace _02_Singleton
{
    public sealed class Singleton1
    {
        private Singleton1()
        {
        }

        private static Singleton1 instance = null;
        public static Singleton1 Instance
        {
            get
            {
                if (instance == null)
                    instance = new Singleton1();

                return instance;
            }
        }
    }

    public sealed class Singleton2
    {
        private Singleton2()
        {
        }

        private static readonly object syncObj = new object();

        private static Singleton2 instance = null;
        public static Singleton2 Instance
        {
            get
            {
                lock (syncObj)
                {
                    if (instance == null)
                        instance = new Singleton2();
                }

                return instance;
            }
        }
    }

    public sealed class Singleton3
    {
        private Singleton3()
        {
        }

        private static object syncObj = new object();

        private static Singleton3 instance = null;
        public static Singleton3 Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncObj)
                    {
                        if (instance == null)
                            instance = new Singleton3();
                    }
                }

                return instance;
            }
        }
    }

    public sealed class Singleton4
    {
        private Singleton4()
        {
            Console.WriteLine("An instance of Singleton4 is created.");
        }

        public static void Print()
        {
            Console.WriteLine("Singleton4 Print");
        }

        private static Singleton4 instance = new Singleton4();
        public static Singleton4 Instance
        {
            get
            {
                return instance;
            }
        }
    }

    public sealed class Singleton5
    {
        Singleton5()
        {
            Console.WriteLine("An instance of Singleton5 is created.");
        }

        public static void Print()
        {
            Console.WriteLine("Singleton5 Print");
        }

        public static Singleton5 Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        class Nested
        {
            static Nested()
            {
            }

            internal static readonly Singleton5 instance = new Singleton5();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 不会打印An instance of Singleton4 is created.
            Singleton4.Print();

            // 不会打印An instance of Singleton5 is created.
            Singleton5.Print();
        }
    }
}

// ------ Output ------
/*
Singleton4 Print
Singleton5 Print
*/

相关推荐

  1. 实现Singleton模式C#

    2024-02-14 18:20:01       48 阅读
  2. C++设计模式 -- 单例(Singleton模式

    2024-02-14 18:20:01       52 阅读
  3. C++ 中的单例模式singleton

    2024-02-14 18:20:01       47 阅读
  4. 单例模式Singleton Pattern)

    2024-02-14 18:20:01       68 阅读
  5. 10-单例模式Singleton

    2024-02-14 18:20:01       59 阅读
  6. 单例设计模式Singleton

    2024-02-14 18:20:01       47 阅读
  7. 单例模式Singleton Pattern)

    2024-02-14 18:20:01       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-14 18:20:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-14 18:20:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-14 18:20:01       82 阅读
  4. Python语言-面向对象

    2024-02-14 18:20:01       91 阅读

热门阅读

  1. Kubernetes的服务质量(QoS)

    2024-02-14 18:20:01       59 阅读
  2. Rust结构体详解:定义、使用及方法

    2024-02-14 18:20:01       51 阅读
  3. C#系列-C#EF框架实现增删改查(27)

    2024-02-14 18:20:01       51 阅读
  4. 大数据的基础探索之大数据时代

    2024-02-14 18:20:01       51 阅读