W
William Stacey
FYI.
/// <summary>
/// Author: William Stacey
/// Fast and simple way to implement a singleton pattern without resorting
/// to nested classes or other static vodo. Can also be easily converted to
/// allow "n" number of instances to be created.
/// </summary>
public class SingletonClass
{
// Vars used by singleton logic.
private static int created = 0; // No object created yet.
private static SingletonClass singletonInstance = null;
// End singleton vars.
// Other private/public vars as needed in class.
private string name;
public SingletonClass()
{
name = "MyName";
Console.WriteLine("Created MyName.");
//other stuff...
}
/// <summary>
/// Returns single instance of SingletonClass if it exists. Otherwise
/// create it, store the reference and return the singleton.
/// This uses very fast Interlocked method that does its work via CPU
/// instructions in an atomic way. Almost as fast as an "if" test, while
/// being thread safe and not requiring kernel mode switch or other
locking.
/// </summary>
/// <returns>The singleton object.</returns>
public static SingletonClass GetInstance()
{
if ( Interlocked.Exchange(ref created, 1) == 1 )
return singletonInstance;
singletonInstance = new SingletonClass();
return singletonInstance;
}
/// <summary>
/// Reset "created" to zero. Next call to GetInstance() will create
/// a new SingletonClass obj. Can be used as to "refresh" and create
/// a new object daily or per some other event. This method is thread
safe.
/// </summary>
public static void ResetSingleton()
{
Interlocked.Exchange(ref created, 0);
}
public string Name
{
get { return this.name; }
}
}
/// <summary>
/// Author: William Stacey
/// Fast and simple way to implement a singleton pattern without resorting
/// to nested classes or other static vodo. Can also be easily converted to
/// allow "n" number of instances to be created.
/// </summary>
public class SingletonClass
{
// Vars used by singleton logic.
private static int created = 0; // No object created yet.
private static SingletonClass singletonInstance = null;
// End singleton vars.
// Other private/public vars as needed in class.
private string name;
public SingletonClass()
{
name = "MyName";
Console.WriteLine("Created MyName.");
//other stuff...
}
/// <summary>
/// Returns single instance of SingletonClass if it exists. Otherwise
/// create it, store the reference and return the singleton.
/// This uses very fast Interlocked method that does its work via CPU
/// instructions in an atomic way. Almost as fast as an "if" test, while
/// being thread safe and not requiring kernel mode switch or other
locking.
/// </summary>
/// <returns>The singleton object.</returns>
public static SingletonClass GetInstance()
{
if ( Interlocked.Exchange(ref created, 1) == 1 )
return singletonInstance;
singletonInstance = new SingletonClass();
return singletonInstance;
}
/// <summary>
/// Reset "created" to zero. Next call to GetInstance() will create
/// a new SingletonClass obj. Can be used as to "refresh" and create
/// a new object daily or per some other event. This method is thread
safe.
/// </summary>
public static void ResetSingleton()
{
Interlocked.Exchange(ref created, 0);
}
public string Name
{
get { return this.name; }
}
}