diff --git a/Singleton/LazySingleton.cs b/Singleton/LazySingleton.cs new file mode 100644 index 0000000..d0ae1e2 --- /dev/null +++ b/Singleton/LazySingleton.cs @@ -0,0 +1,15 @@ +public sealed class LazySingleton +{ + private LazySingleton() { } + private static readonly Lazy _lazy = new Lazy(() => new LazySingleton()); + + public static LazySingleton GetInstance(string value) + { + var instance = _lazy.Value; + instance.Value = value; + + return instance; + } + + public string Value { get; set; } +} \ No newline at end of file diff --git a/Singleton/NonSingleton.cs b/Singleton/NonSingleton.cs new file mode 100644 index 0000000..1f404f5 --- /dev/null +++ b/Singleton/NonSingleton.cs @@ -0,0 +1,9 @@ +public sealed class NonSingleton +{ + public NonSingleton(string value) + { + Value = value; + } + + public string Value { get; set; } +} \ No newline at end of file diff --git a/Singleton/Program.cs b/Singleton/Program.cs new file mode 100644 index 0000000..ac994be --- /dev/null +++ b/Singleton/Program.cs @@ -0,0 +1,37 @@ +var nonSingleton1 = new NonSingleton("A"); +var nonSingleton2 = new NonSingleton("B"); +Console.WriteLine(nonSingleton1.Value); +Console.WriteLine(nonSingleton2.Value); +TestSingleton(nonSingleton1, nonSingleton2); + +var simpleSingleton1 = SimpleSingleton.GetInstance("A"); +var simpleSingleton2 = SimpleSingleton.GetInstance("B"); +Console.WriteLine(simpleSingleton1.Value); +Console.WriteLine(simpleSingleton2.Value); +TestSingleton(simpleSingleton1, simpleSingleton2); + + +var threadSafeSingleton1 = ThreadSafeSingleton.GetInstance("A"); +var threadSafeSingleton2 = ThreadSafeSingleton.GetInstance("B"); +Console.WriteLine(threadSafeSingleton1.Value); +Console.WriteLine(threadSafeSingleton2.Value); +TestSingleton(threadSafeSingleton1, threadSafeSingleton2); + + +var lazySingleton1 = LazySingleton.GetInstance("A"); +var lazySingleton2 = LazySingleton.GetInstance("B"); +Console.WriteLine(lazySingleton1.Value); +Console.WriteLine(lazySingleton2.Value); +TestSingleton(lazySingleton1, lazySingleton2); + +static void TestSingleton(T object1, T object2) where T : class +{ + if (object1 == object2) + { + Console.WriteLine($"{typeof(T).FullName}: These are singletons"); + } + else + { + Console.WriteLine($"{typeof(T).FullName}: These are NOT singletons"); + } +} \ No newline at end of file diff --git a/Singleton/SimpleSingleton.cs b/Singleton/SimpleSingleton.cs new file mode 100644 index 0000000..52b6c18 --- /dev/null +++ b/Singleton/SimpleSingleton.cs @@ -0,0 +1,18 @@ +public sealed class SimpleSingleton +{ + private SimpleSingleton() { } + + private static SimpleSingleton _instance; + + public static SimpleSingleton GetInstance(string value) + { + if (_instance == null) + { + _instance = new SimpleSingleton(); + _instance.Value = value; + } + return _instance; + } + + public string Value { get; set; } +} diff --git a/Singleton/Singleton.csproj b/Singleton/Singleton.csproj new file mode 100644 index 0000000..d439800 --- /dev/null +++ b/Singleton/Singleton.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/Singleton/ThreadSafeSingleton.cs b/Singleton/ThreadSafeSingleton.cs new file mode 100644 index 0000000..c21c1a6 --- /dev/null +++ b/Singleton/ThreadSafeSingleton.cs @@ -0,0 +1,29 @@ +using System; +using System.Threading; + +public sealed class ThreadSafeSingleton +{ + private ThreadSafeSingleton() { } + + private static readonly object _lock = new object(); + + private static ThreadSafeSingleton _instance; + + public static ThreadSafeSingleton GetInstance(string value) + { + if (_instance == null) + { + lock (_lock) + { + if (_instance == null) + { + _instance = new ThreadSafeSingleton(); + _instance.Value = value; + } + } + } + return _instance; + } + + public string Value { get; set; } +}