Added Tutorial 9
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace _158326_Week_7_Singletons
|
||||
@@ -10,6 +11,58 @@ namespace _158326_Week_7_Singletons
|
||||
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)");
|
||||
Console.WriteLine("\nCompany:");
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
Company instanceOfCompany = Company.GetCompany();
|
||||
Console.WriteLine(instanceOfCompany.ShowDetails());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
Console.WriteLine("\nFileSingleton:");
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
FileSingleton instanceOfCompany = FileSingleton.GetInstance(i);
|
||||
Console.WriteLine(instanceOfCompany.ShowDetails());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
for (int i = 100; i < 110; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
FileSingleton instanceOfCompany = FileSingleton.GetInstance(i);
|
||||
Console.WriteLine(instanceOfCompany.ShowDetails());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
Console.WriteLine("\nCompany:");
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
Company instanceOfCompany = Company.GetCompany();
|
||||
Console.WriteLine(instanceOfCompany.ShowDetails());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +76,15 @@ namespace _158326_Week_7_Singletons
|
||||
/// Store the instance in a private, static variable
|
||||
/// </summary>
|
||||
private static FileSingleton instance;
|
||||
private string _name;
|
||||
|
||||
/// <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
|
||||
//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
|
||||
// public FileStream OpenedFileStream { get; set; } // Calling FileSingleton.GetInstance().OpenedFileStream will always return this object
|
||||
|
||||
|
||||
// We can also define
|
||||
@@ -40,20 +94,68 @@ namespace _158326_Week_7_Singletons
|
||||
/// 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() { }
|
||||
private FileSingleton(int i)
|
||||
{
|
||||
_name = i.ToString();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the initialised instance of FileSingleton
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static FileSingleton GetInstance()
|
||||
public static FileSingleton GetInstance(int i)
|
||||
{
|
||||
if (instance is null)
|
||||
{
|
||||
instance = new FileSingleton(i);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public string ShowDetails()
|
||||
{
|
||||
if (instance is null)
|
||||
return _name;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Company
|
||||
{
|
||||
private string _name;
|
||||
private static Company _company;
|
||||
private bool _legalInstance;
|
||||
private List<string> _listEmployee;
|
||||
|
||||
private Company()
|
||||
{
|
||||
if (_company == null)
|
||||
{
|
||||
instance = new FileSingleton();
|
||||
_company = this;
|
||||
_name = "test";
|
||||
_legalInstance = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_legalInstance = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Company GetCompany()
|
||||
{
|
||||
return new Company();
|
||||
}
|
||||
|
||||
public string ShowDetails()
|
||||
{
|
||||
if (_legalInstance)
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ApplicationException("Null Object");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user