Untitled
unknown
plain_text
2 years ago
6.6 kB
6
Indexable
using System.Reflection.Metadata; using static System.Net.Mime.MediaTypeNames; namespace LaborationCC.Classes //När man skriver till en fil vill man bara att en enda instans gör det, eller att man använder låset till att en enda gör det. { internal class PlayerDataStorage : IPlayerDataStorage { private static PlayerDataStorage instance; private static readonly object lockObject = new object(); private readonly string filePath; private readonly IIO _io; //Måste lägga till IPlayerDataStorage som en parameter till constructorn //private readonly IIO io; private PlayerDataStorage(string path, IIO io) //Använd IIO och ConsoleIO i PlayerDataStorage istället för Console.ReadLine och Console.WriteLine. Private; nu kommer den bara skapas på rad 33. Ingen annan kan skapa en sådan. { filePath = path; _io = io; } public static PlayerDataStorage Instance //genom den man får tillgång till playerdatastorage, den hade ingen referens tidigare. { get { lock (lockObject) { if (instance == null) { instance = new PlayerDataStorage("result.txt", new ConsoleIO()); } return instance; } } } public void Write(string name, int guesses) { lock (lockObject) { using (StreamWriter output = new StreamWriter(filePath, append: true)) { output.WriteLine(name + "#&#" + guesses); } } } public void ClearData() { lock (lockObject) { _io.WriteLine("Are you sure you want to clear all player data? (y/n)"); //Använd IIO och ConsoleIO i PlayerDataStorage istället för Console.ReadLine och Console.WriteLine string input = _io.ReadLine(); if (input?.ToLower() == "y") { // Clear the player data using (StreamWriter writer = new StreamWriter(filePath)) { writer.Write(string.Empty); } _io.WriteLine("Player data cleared successfully."); } else { _io.WriteLine("Player data was not cleared."); } } } public void ShowTopList() { lock (lockObject) { using (StreamReader input = new StreamReader(filePath)) { List<IPlayerData> results = new List<IPlayerData>(); var line = input.ReadLine(); while (line != null) { string[] nameAndScore = line.Split(new string[] { "#&#" }, StringSplitOptions.None); string name = nameAndScore[0]; int guesses = Convert.ToInt32(nameAndScore[1]); IPlayerData pd = new PlayerData(name, guesses); int pos = results.IndexOf(pd); if (pos < 0) { results.Add(pd); } else { results[pos].Update(guesses); } } results.Sort((p1, p2) => p1.Average().CompareTo(p2.Average())); _io.WriteLine("Player Games Average"); foreach (IPlayerData p in results) { _io.WriteLine(string.Format("{0,-9}{1,5:D}{2,9:F2}", p.Name, p.NGames, p.Average())); } } } } } } //In the provided code, the Instance property of the PlayerDataStorage class is written with a capital 'I' to follow the naming convention commonly used for singleton instances in C#. //By convention, when creating a singleton instance, it is common to use the name "Instance" with a capital 'I' as a property name. This convention helps to distinguish the singleton instance from other properties or variables within the class. //The use of a singleton pattern allows for the creation of only one instance of the class and provides a global point of access to that instance. In this case, the Instance property ensures that only one instance of the PlayerDataStorage class is created and returned when accessed. //Note that the use of a singleton pattern and the specific naming convention for the instance property are not enforced by the C# language itself but rather a convention followed by developers for clarity and consistency. //In the provided code, the Instance property is used to implement the Singleton design pattern. The purpose of the Singleton pattern is to ensure that only one instance of a class is created and provide a global point of access to that instance. //By using the Instance property, the PlayerDataStorage class guarantees that there is only a single instance of the class throughout the application.When accessed for the first time, the Instance property checks if the instance variable is null and creates a new instance of PlayerDataStorage if it is.Subsequent calls to Instance will return the already created instance. //The difference between instance with a lowercase 'i' and Instance with an uppercase 'I' lies in their usage and scope. The instance variable is a private static field within the PlayerDataStorage class. It holds the reference to the single instance of PlayerDataStorage that is created. It is only accessed and modified within the PlayerDataStorage class. //On the other hand, the Instance property is a public static property that provides the global access point to the single instance of PlayerDataStorage. It can be accessed from other classes in the application to obtain the singleton instance. //The capitalization of 'I' in Instance is a common convention used to indicate that it is a public property representing the singleton instance. It helps to differentiate it from the private instance variable and other properties or variables in the class.
Editor is loading...