Skip to content

Instantly share code, notes, and snippets.

@koturn
Created June 15, 2020 21:11
Show Gist options
  • Save koturn/f2457e02b35904fde4399f33767e410a to your computer and use it in GitHub Desktop.
Save koturn/f2457e02b35904fde4399f33767e410a to your computer and use it in GitHub Desktop.
様々なシングルトンの実装
using System;
namespace Wandbox
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Foo.Instance.Name);
Console.WriteLine(Bar.Instance.Name);
Console.WriteLine(Baz.Instance.Name);
Console.WriteLine(Qux.Instance.Name);
}
}
/// <summary>
/// First implementation of singleton.
/// </summary>
public sealed class Foo
{
/// <summary>
/// Get the only instance of <see cref="Foo"/> class.
/// </summary>
public static Foo Instance { get; }
/// <summary>
/// Create the only instance of <see cref="Foo"/> class.
/// </summary>
static Foo()
{
Instance = new Foo();
}
/// <summary>
/// Name of this instance.
/// </summary>
public string Name { get; }
/// <summary>
/// Initializes a new instance of <see cref="Foo"/> class.
/// </summary>
private Foo()
{
Name = nameof(Foo);
}
} // class Hoge
/// <summary>
/// Second test implementation of singleton.
/// </summary>
public sealed class Bar
{
/// <summary>
/// The only instance of <see cref="Bar"/> class.
/// </summary>
private static Bar _instance;
/// <summary>
/// Get the only instance of <see cref="Bar"/> class.
/// </summary>
public static Bar Instance => _instance ??= new Bar();
/// <summary>
/// Name of this instance.
/// </summary>
public string Name { get; }
/// <summary>
/// Initializes a new instance of <see cref="Bar"/> class.
/// </summary>
private Bar()
{
Name = nameof(Bar);
}
} // class Fuga
/// <summary>
/// Third implementation of singleton.
/// </summary>
public sealed class Baz : SingletonLazy<Baz>
{
/// <summary>
/// Name of this instance.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Initializes a new instance of <see cref="Baz"/> class.
/// </summary>
private Baz()
{
Name = nameof(Baz);
}
} // class Baz
/// <summary>
/// Fourth implementation of singleton.
/// </summary>
public sealed class Qux : SingletonLazy<Baz>
{
/// <summary>
/// Name of this instance.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Initializes a new instance of <see cref="Qux"/> class.
/// </summary>
private Qux()
{
Name = nameof(Qux);
}
} // class Qux
/// <summary>
/// Generic implementation of singleton class.
/// </summary>
/// <example>
/// <code>
/// public class Foo : Singleton&lt;Foo&gt;
/// {
/// public string Name { get; set; } = "Foo";
/// }
/// </code>
/// </example>
/// <typeparam name="T">Type of actual singleton class.</typeparam>
public abstract class Singleton<T>
where T : class
{
/// <summary>
/// Get the only instance of <typeparamref name="T"/> class.
/// </summary>
public static T Instance { get; }
/// <summary>
/// Create the only instance of <typeparamref name="T"/> class.
/// </summary>
static Singleton()
{
Instance = (T)Activator.CreateInstance(typeof(T), true);
}
} // class Singleton<T>
/// <summary>
/// Generic implementation of singleton class.
/// </summary>
/// <example>
/// <code>
/// public class Foo : SingletonLazy&lt;Foo&gt;
/// {
/// public string Name { get; set; } = "Foo";
/// }
/// </code>
/// </example>
/// <typeparam name="T">Type of actual singleton class.</typeparam>
public abstract class SingletonLazy<T>
where T : class
{
/// <summary>
/// The only instance of <typeparamref name="T"/> class.
/// </summary>
private static T _instance;
/// <summary>
/// Get the singleton instance of <typeparamref name="T"/> class.
/// </summary>
public static T Instance => _instance ??= (T)Activator.CreateInstance(typeof(T), true);
} // class SingletonLazy<T>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment