diff --git a/158.326 Software Architecture.sln b/158.326 Software Architecture.sln new file mode 100644 index 0000000..28bd4bd --- /dev/null +++ b/158.326 Software Architecture.sln @@ -0,0 +1,13 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31515.178 +MinimumVisualStudioVersion = 10.0.40219.1 +Global + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A613E761-D85D-4A76-8EFC-5A095B348A49} + EndGlobalSection +EndGlobal diff --git a/158326 Week 7 Singletons/158326 Week 7 Singletons.csproj b/158326 Week 7 Singletons/158326 Week 7 Singletons.csproj new file mode 100644 index 0000000..21e06f5 --- /dev/null +++ b/158326 Week 7 Singletons/158326 Week 7 Singletons.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + _158326_Week_7_Singletons + + + diff --git a/158326 Week 7 Singletons/158326 Week 7 Singletons.sln b/158326 Week 7 Singletons/158326 Week 7 Singletons.sln new file mode 100644 index 0000000..fc91685 --- /dev/null +++ b/158326 Week 7 Singletons/158326 Week 7 Singletons.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31515.178 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "158326 Week 7 Singletons", "158326 Week 7 Singletons.csproj", "{0540B959-6D66-4CBD-A18F-74344B782EF8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0540B959-6D66-4CBD-A18F-74344B782EF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0540B959-6D66-4CBD-A18F-74344B782EF8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0540B959-6D66-4CBD-A18F-74344B782EF8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0540B959-6D66-4CBD-A18F-74344B782EF8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2ED746F5-A191-4E18-B64C-A78216BC01E7} + EndGlobalSection +EndGlobal diff --git a/158326 Week 7 Singletons/Program.cs b/158326 Week 7 Singletons/Program.cs new file mode 100644 index 0000000..0630e10 --- /dev/null +++ b/158326 Week 7 Singletons/Program.cs @@ -0,0 +1,59 @@ +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)"); + } + } + + /// + /// 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) + /// + sealed class FileSingleton + { + /// + /// Store the instance in a private, static variable + /// + private static FileSingleton instance; + + /// + /// Path of the file to open + /// + 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 + + /// + /// 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 + /// + private FileSingleton() { } + + + /// + /// Gets the initialised instance of FileSingleton + /// + /// + public static FileSingleton GetInstance() + { + if (instance is null) + { + instance = new FileSingleton(); + } + return instance; + } + } +}