zoukankan      html  css  js  c++  java
  • 《大话设计模式》学习笔记17:单例模式

      

      

    实现:

    1.Singleton:

        public class Singleton
        {
            private static Singleton instance;
            //构造方法为private,外部代码不能直接实例化
            private Singleton()
            {
    
            }
    
            public static Singleton GetInstance()
            {
                if(instance==null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }

    2.客户端代码:

        class Program
        {
            static void Main(string[] args)
            {
                Singleton singleton1 = Singleton.GetInstance();
                Singleton singleton2 = Singleton.GetInstance();
    
                if(singleton1==singleton2)
                {
                    Console.WriteLine("两个对象是相同的实例。");
                }
                else
                {
                    Console.WriteLine("两个对象是不同的实例。");
                }
            }
        }

    多线程时的单例:

    1.lock:

        public class Singleton
        {
            private static Singleton instance;
            private static readonly object syncRoot = new object();
            private Singleton()
            {
    
            }
    
            public static Singleton GetInstance()
            {
                lock(syncRoot)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
                return instance;
            }
        }

    2.双重锁定:

        public class Singleton
        {
            private static Singleton instance;
            private static readonly object syncRoot = new object();
            private Singleton()
            {
    
            }
    
            public static Singleton GetInstance()
            {
                if(instance==null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new Singleton();
                        }
                    }
                }
                return instance;
            }
        }

    静态初始化:

        public sealed class Singleton//sealed阻止发生派生,因为派生可能会增加实例
        {
            private static readonly Singleton instance = new Singleton();
            private Singleton()
            {
    
            }
    
            public static Singleton GetInstance()
            {
                return instance;
            }
        }

    静态初始化的方式在自己被加载时就将自己实例化,称之为恶汉式单例类;之前的单例模式是在第一次被引用时才会将自己实例化,称为懒汉单例类。

  • 相关阅读:
    ---Install Oracle Java 11 SE under Ubuntu
    ---个人英语单词收集!
    ---Android alarm使用
    ---FLAG_NO_CREATE 的用途!
    ---Englist Word Memo
    ---Ubuntu安装后要做的几件重要的事情 (适合Ubuntu机子的开发人员)
    ---Android egl/egls 概念
    --- Checking fs of the mounted partitions on Android device
    移动应用论坛——如何“玩赚”微信
    Android复制assets目录下的图片到内存
  • 原文地址:https://www.cnblogs.com/walden1024/p/4520960.html
Copyright © 2011-2022 走看看