0

I'm attempting to make a single XML Manager class that any of my other classes can access to save, load, or delete information on demand. Due to the nature of this class, only one of them should exist at any given time and be accessible by any class.

I've looked into the difference between static and singleton classes but, I'm not sure which I should implement or how I should implement them.

So far I have a standard class that I'm not sure where I should go with from here:

public class XMLManager
    {
        //List of items
        public ItemDatabase itemDB;

        //Save Function
        public void SaveItems()
        {
            //Create a new XML File
            XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
            FileStream stream = new FileStream(Application.dataPath + "/StreamingFiles/XML/item_data.xml", FileMode.Create);
            serializer.Serialize(stream, itemDB);
            stream.Close();
        }

        //Load Function
        public void LoadItems()
        {
            //Open an XML File
            if (File.Exists(Application.dataPath + "/StreamingFiles/XML/item_data.xml"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
                FileStream stream = new FileStream(Application.dataPath + "/StreamingFiles/XML/item_data.xml", FileMode.Open);
                itemDB = serializer.Deserialize(stream) as ItemDatabase;
                stream.Close();
            }
            else
            {
                Debug.LogError("File not found!");
            }
        }

        //Delete Function
        public void DeleteItems()
        {
            //Delete an XML File
            File.Delete(Application.dataPath + "/StreamingFiles/XML/item_data.xml");
        }
    }

    [System.Serializable]
    public class ItemEntry
    {
        public string ItemName;
        public SaveMaterial material;
        public int Value;
    }

    [System.Serializable]
    public class ItemDatabase
    {
        [XmlArray("CombatEquipment")]
        public List<ItemEntry> list = new List<ItemEntry>();
    }

    public enum SaveMaterial
    {
        Wood,
        Copper,
        Iron,
        Steel,
        Obsidian
    }

Just for the record, I understand that this may not be the most perfect way of going about this problem, however, this is the way that works for me in my use case.

Toxic Cookie
  • 73
  • 1
  • 7
  • The singleton pattern is probably preferable to a static class, especially if you need to maintain any kind of state within the object. Have you tried to research and implement that? You can find C#-specific examples of the pattern. Where are you stuck on it, exactly? – ADyson Oct 27 '19 at 23:41
  • I'm trying to figure out which would be preferable and how I should implement it accordingly to this use case. – Toxic Cookie Oct 27 '19 at 23:50
  • Well, that's my advice, above. It will always be somewhat a matter of opinion though. For your relatively simple class above, it probably doesn't actually matter much. But if it gets more complicated in future then a static class could start to cause issues, depending on what you extra functionality you end up needing. – ADyson Oct 27 '19 at 23:52
  • If you choose to implement the singleton pattern, then you take your class as above, make the constructor private and then have a static method in the class whose job is to either fetch the current instance of the class which it's holding (in other static variable) or create a new instance if one hasn't already been created, and then return that. The static method is the only public thing in the class. like I said, you can find lots of examples of the pattern (it's always basically the same) if you search online. – ADyson Oct 27 '19 at 23:54
  • 1
    Are you using an IoC container? – mjwills Oct 27 '19 at 23:55
  • Alright, I see what you mean. Also, I haven't a clue what an IoC container is haha. – Toxic Cookie Oct 28 '19 at 00:00
  • I'd suggest reading up on Autofac. – mjwills Oct 28 '19 at 01:08

1 Answers1

1

Depends what are you trying to get out of that class. Both static and singleton are doing almost the same job with a little difference. The main differences are emphasized here Difference between static class and singleton pattern?.

Unless you want your class to implement classes, and you want to keep it simple, go with Static.

Take the example of the methods you are using for producing Logs or working with files.

Debug.LogError("Some String");

File.Delete("FullPathToFile");

They are also Static classes. See Debug Class and File Class.

Attila Antal
  • 811
  • 8
  • 17