• CSharp: Singleton Pattern in donet 6


        /// <summary>
        ///单例模式 单件模式、Singleton  Pattern 
        /// </summary>
        public abstract class BaseGreeter
        {
            public virtual void Greet() =>
                Console.WriteLine($"Greetings from {GetType().Name}!");
        }
    
    
     /// <summary>
        /// 单例模式 单件模式、Singleton  Pattern 
        /// </summary>
        public class SimpleGreeter : BaseGreeter
        {
            /// <summary>
            /// 
            /// </summary>
            private static SimpleGreeter? _instance;
            /// <summary>
            /// 
            /// </summary>
            private SimpleGreeter()
            {
                Console.WriteLine("简单的单例实例化了第一次也是唯一一次!!");
            }
            /// <summary>
            /// 
            /// </summary>
            public static SimpleGreeter Instance
            {
                get
                {
                    Console.WriteLine("请求简单的单例...");
    
                    if (_instance == null)
                    {
                        _instance = new SimpleGreeter();
                    }
    
                    Console.WriteLine("返回简单单例");
                    return _instance;
                }
            }
        }
    
        /// <summary>
        /// 单例模式 单件模式、Singleton  Pattern 
        /// </summary>
        public class SimpleThreadSafetyGreeter : BaseGreeter
        {
    
            /// <summary>
            /// 
            /// </summary>
            private static readonly object Padlock = new();
    
          /// <summary>
          /// 
          /// </summary>
            private static SimpleThreadSafetyGreeter? _instance;
            /// <summary>
            /// 
            /// </summary>
            private SimpleThreadSafetyGreeter()
            {
            }
            /// <summary>
            /// 
            /// </summary>
            public static SimpleThreadSafetyGreeter Instance
            {
                get
                {
                    lock (Padlock)
                    {
                        if (_instance == null)
                        {
                            _instance = new SimpleThreadSafetyGreeter();
                        }
    
                        return _instance;
                    }
                }
            }
        }
    
    
     /// <summary>
        /// 单例模式 单件模式、Singleton  Pattern 
        /// </summary>
        public class DotNetLazyGreeter : BaseGreeter
        {
    
            /// <summary>
            /// 
            /// </summary>
            private static readonly Lazy<DotNetLazyGreeter> Lazy = new(() => new DotNetLazyGreeter());
            /// <summary>
            /// 
            /// </summary>
            private DotNetLazyGreeter()
            {
            }
            /// <summary>
            /// 
            /// </summary>
            public static DotNetLazyGreeter Instance => Lazy.Value;
        }
    
      /// <summary>
        /// 单例模式 单件模式、Singleton  Pattern 
        /// </summary>
        public class DoubleCheckGreeter : BaseGreeter
        {
    
            /// <summary>
            /// /
            /// </summary>
            private static readonly object Padlock = new();
            /// <summary>
            /// 
            /// </summary>
            private static DoubleCheckGreeter? _instance;
            /// <summary>
            /// 
            /// </summary>
            private DoubleCheckGreeter()
            {
            }
    
    
            /// <summary>
            /// Gets instance in a way to support multi-threaded applications through 'double checked locking'
            /// pattern which (once the instance exists) avoids locking each time the method is invoked.
            /// </summary>
            [SuppressMessage("Maintainability", "CA1508:Avoid dead conditional code",
                Justification = "If the first check determines that instance is null, that might not be the case for the second check in multi-threaded scenarios.")]     
           
            public static DoubleCheckGreeter Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        lock (Padlock)
                        {
                            if (_instance == null)
                            {
                                _instance = new DoubleCheckGreeter();
                            }
                        }
                    }
    
                    return _instance;
                }
            }
        }
    
     /// <summary>
        /// 单例模式 单件模式、Singleton  Pattern 
        /// </summary>
        public class LocklessFullyLazyGreeter : BaseGreeter
        {
    
            /// <summary>
            /// 
            /// </summary>
            private LocklessFullyLazyGreeter()
            {
            }
            /// <summary>
            /// 
            /// </summary>
            public static LocklessFullyLazyGreeter Instance => Nested.Instance;
            /// <summary>
            /// 
            /// </summary>
            private class Nested
            {
                /// <summary>
                /// Although nested classes have access to the enclosing class's private members,
                /// the reverse is not true, hence the need for instance to be internal here.
                /// </summary>
                internal static readonly LocklessFullyLazyGreeter Instance = new();
    
                // Explicit static constructor to tell C# compiler not to mark as BeforeFieldInit.
                // http://csharpindepth.com/Articles/General/Singleton.aspx
                static Nested()
                {
                }
            }
        }
    
    
        /// <summary>
        /// 单例模式 单件模式、Singleton  Pattern 
        /// </summary>
        public class LocklessGreeter : BaseGreeter
                {
                    /// <summary>
                    /// 
                    /// </summary>
                    private static readonly LocklessGreeter InstanceValue = new();
    
                    // Explicit static constructor to tell C# compiler not to mark type as BeforeFieldInit.
                    static LocklessGreeter()
                    {
                    }
                     /// <summary>
                     /// 
                     /// </summary>
                    private LocklessGreeter()
                    {
                    }
                    /// <summary>
                    /// 
                    /// </summary>
                    public static LocklessGreeter Instance => InstanceValue;
             }
    }
    

      

    调用:

               Geovin.Du.DuSingleton.Types.SimpleGreeter.Instance.Greet();
                Geovin.Du.DuSingleton.Types.SimpleThreadSafetyGreeter.Instance.Greet();
                Geovin.Du.DuSingleton.Types.DoubleCheckGreeter.Instance.Greet();
                Geovin.Du.DuSingleton.Types.LocklessGreeter.Instance.Greet();
                Geovin.Du.DuSingleton.Types.LocklessFullyLazyGreeter.Instance.Greet();
                Geovin.Du.DuSingleton.Types.DotNetLazyGreeter.Instance.Greet();
    

      

    输出:

    2
    最初的问候...
    
    请求简单的单例...
    返回简单单例
    Greetings from SimpleGreeter!
    Greetings from SimpleThreadSafetyGreeter!
    Greetings from DoubleCheckGreeter!
    Greetings from LocklessGreeter!
    Greetings from LocklessFullyLazyGreeter!
    Greetings from DotNetLazyGreeter!
    
    再见的问候...
    
    请求简单的单例...
    返回简单单例
    Greetings from SimpleGreeter!
    Greetings from SimpleThreadSafetyGreeter!
    Greetings from DoubleCheckGreeter!
    Greetings from LocklessGreeter!
    Greetings from LocklessFullyLazyGreeter!
    Greetings from DotNetLazyGreeter!
    

      

    萬事萬物並不是存在就有意義的。顯然,人、動物和物體以物的形式存在著,並且會有不同程度的改變,但是,賦予它們意義的並非單純的物質上的存在。要獲得確定的意義,就必須賦予人、動物和物體以象徵意味。只有把生命和無生命的棲居者都轉換為象徵性的實體,社會與文化才能理解這個世界—即使只是嘗試性的、暫時性的理解。人們使用的象徵符號多種多樣,其含義因社會和文化的差異而發生變化。此類象徵符號、囊括了塑造特定群體之價值體系和行為方式的詞語、視覺形象以及禮儀和習俗。人、動物和物體皆與象徵符號相關,正是在這一點上,一個群體經由教育而認識到自身的存在是意味深長或意義重大的。
    同時,任何物質形式都不可能擁有一個最終或者永恆的意義。事實上,隨著它所屬的文化或社會形態的改變,其含義將不可避免地發生與變化。這種說法如果適用於具體的實體,並且會因此發生變化,這無疑已被用來意指抽象概念的詞語的變化所證實—諸如“文化”、“社會”、“價值”和“社群”等。此類觀念並沒有指向普遍性的範疇,毋寧說,它們把握住了特定語境中的意義,而意義取決於那些用來界定意義的象徵符號。
    ---《Critical and Cultural Theory》 by Dani Cavallaro

  • 相关阅读:
    《DSP using MATLAB》Problem 6.17
    一些老物件
    《DSP using MATLAB》Problem 6.16
    《DSP using MATLAB》Problem 6.15
    《DSP using MATLAB》Problem 6.14
    《DSP using MATLAB》Problem 6.13
    《DSP using MATLAB》Problem 6.12
    《DSP using MATLAB》Problem 6.11
    P1414 又是毕业季II
    Trie树
  • 原文地址:https://www.cnblogs.com/geovindu/p/16875562.html
Copyright © 2020-2023  润新知