Hey everyone,
I am trying to make sure I am writing my classes in the best manner possible (program to an interface not an implementation, using good oop practices, open-closed principle, etc). I believe I am on the right track with that I am doing, but I have question as to a specific part in my class, as I feel like I will be writing duplicate code between classes.
I have a class called MissionEncounter which implemented the IEncounter interface. The code is below.
public class MissionEncounter : IEncounter<Mission>
{
private RandomizedList<Mission> missions = new RandomizedList<Mission>();
private string path = "Assets/Resources/XML Files/Activities/Missions";
public MissionEncounter()
{
string[] files = Reader.GetXmlFiles(path);
Create(files, missions);
missions.Shuffle();
}
public Mission GetNext()
{
return missions.Next();
}
private void Create(string[] files, RandomizedList<Mission> list)
{
for (int i = 0; i < files.Length; i++)
{
//...use XML to retrieve Encounter(Mission in this case) specific information
//...create a new Mission(...)
//...add new Mission to the missions list
}
}
}
public interface IEncounter<T>
{
T GetNext();
void Create(string[] files, RandomizedList<T> list);
}
I will have multiple types of encounters in my game, some are missions, some are shops, random loot, neutral/hostile ships, and more. I was thinking I had to create a new ________Encounter class for each type of encounter. (LootEncounter, ShopEncounter, etc). The reason for this is that each encounter types details are defined by an XML file structure and there are multiples of each (100 missions, 3 shops, etc). Using the XML format allows me to easily add new content without having to write new code - unless I want to create a totally new encounter type.
What is bugging me is that the _______Encounter class would all be duplicates of each other except for the code inside the method that creates the encounter type. Create(string[] files, RandomizedList<T> list).
I was trying to stay away from inheritance as much as possible, but would it be acceptable in this situation to have MissionEncounter inherit from some type of generic base class that implements IEncounter so that it takes care of class construction, the GetNext() method, and implements an overridable Create(..) method?