Add singleton example

This commit is contained in:
Alex Hyett 2023-09-04 11:29:51 +01:00
parent 27045cc4b9
commit 1454650156
6 changed files with 118 additions and 0 deletions

View file

@ -0,0 +1,15 @@
public sealed class LazySingleton
{
private LazySingleton() { }
private static readonly Lazy<LazySingleton> _lazy = new Lazy<LazySingleton>(() => new LazySingleton());
public static LazySingleton GetInstance(string value)
{
var instance = _lazy.Value;
instance.Value = value;
return instance;
}
public string Value { get; set; }
}

View file

@ -0,0 +1,9 @@
public sealed class NonSingleton
{
public NonSingleton(string value)
{
Value = value;
}
public string Value { get; set; }
}

37
Singleton/Program.cs Normal file
View file

@ -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>(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");
}
}

View file

@ -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; }
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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; }
}