diff --git a/Creational/DesignPatterns.Singleton/SingletonWithLazy.cs b/Creational/DesignPatterns.Singleton/SingletonWithLazy.cs index 4484082..603c3d6 100644 --- a/Creational/DesignPatterns.Singleton/SingletonWithLazy.cs +++ b/Creational/DesignPatterns.Singleton/SingletonWithLazy.cs @@ -1,8 +1,8 @@ namespace DesignPatterns.Singleton; -public class SingletonWithLazy +public sealed class SingletonWithLazy { - private static readonly Lazy instance = new(() => new SingletonWithLazy()); + private static readonly Lazy LazyInstance = new(() => new SingletonWithLazy()); /// /// The private constructor is declared to prevent instantiation directly, via calling a default constructor. @@ -12,5 +12,5 @@ private SingletonWithLazy() Console.WriteLine($"An instance of {nameof(SingletonWithLazy)} has been created."); } - public static SingletonWithLazy Instance => instance.Value; + public static SingletonWithLazy Instance => LazyInstance.Value; } diff --git a/Creational/DesignPatterns.Singleton/SingletonWithLock.cs b/Creational/DesignPatterns.Singleton/SingletonWithLock.cs index d12c870..087791f 100644 --- a/Creational/DesignPatterns.Singleton/SingletonWithLock.cs +++ b/Creational/DesignPatterns.Singleton/SingletonWithLock.cs @@ -1,16 +1,18 @@ -namespace DesignPatterns.Singleton; +using System.Diagnostics; -public class SingletonWithLock +namespace DesignPatterns.Singleton; + +public sealed class SingletonWithLock { private static SingletonWithLock? instance = null; - private static readonly object lockObj = new(); + private static readonly object LockObject = new(); /// /// The private constructor is declared to prevent instantiation directly, via calling a default constructor. /// private SingletonWithLock() { - Console.WriteLine($"An instance of {nameof(SingletonWithLock)} has been created."); + Debug.WriteLine($"An instance of {nameof(SingletonWithLock)} has been created."); } public static SingletonWithLock Instance @@ -19,7 +21,7 @@ public static SingletonWithLock Instance { // The cost of executing the lock operation is significantly higher // in comparison to the straightforward pointer check `instance != null`. - lock (lockObj) + lock (LockObject) { return instance ??= new SingletonWithLock(); }