60 lines
2.1 KiB
C#
60 lines
2.1 KiB
C#
|
using System;
|
|||
|
using System.IO;
|
|||
|
|
|||
|
namespace _158326_Week_7_Singletons
|
|||
|
{
|
|||
|
class Program
|
|||
|
{
|
|||
|
static void Main(string[] args)
|
|||
|
{
|
|||
|
Console.WriteLine("158.326 Software Architecture");
|
|||
|
Console.WriteLine("\tWeek 7 - Singleton Design Pattern");
|
|||
|
Console.WriteLine("\tSingletons are instanced classes that can have only 0 or 1 instances of that class in existance in the application (Multitons are a variant that restricts the maximum number of instances)");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Represents a singleton file stream; useful for logs etc., where access to a specific file should be performed singularly
|
|||
|
/// (don't open multiple streams on the same file)
|
|||
|
/// </summary>
|
|||
|
sealed class FileSingleton
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Store the instance in a private, static variable
|
|||
|
/// </summary>
|
|||
|
private static FileSingleton instance;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Path of the file to open
|
|||
|
/// </summary>
|
|||
|
public static Uri FilePath { get; set; } = new Uri("\\log.txt", UriKind.Relative); // Opens exactly one file stream on log.txt and will always return that stream
|
|||
|
|
|||
|
// We can define instance variables here:
|
|||
|
public FileStream OpenedFileStream { get; set; } // Calling FileSingleton.GetInstance().OpenedFileStream will always return this object
|
|||
|
|
|||
|
|
|||
|
// We can also define
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// The default constructor must be implemented, and as private so only functions in this class may call it
|
|||
|
/// This restricts the creation of this instance to local methods, so by restricting constructor calls
|
|||
|
/// to GetInstance(), we can ensure that only one instance is created
|
|||
|
/// </summary>
|
|||
|
private FileSingleton() { }
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Gets the initialised instance of FileSingleton
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public static FileSingleton GetInstance()
|
|||
|
{
|
|||
|
if (instance is null)
|
|||
|
{
|
|||
|
instance = new FileSingleton();
|
|||
|
}
|
|||
|
return instance;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|