I'm working in a C# codebase that has a class hierarchy:
class Animal { Animal prey; }
class Mammal : Animal { Mammal[] livesAmicablyWith; }
class Lion : Mammal { }
Forgive the stupid example.
I would like to repurpose this class hierarchy for something representable in the same exact object format, but which requires more data. In my ideal world, it would look like this:
class Animal { Animal prey; string moreAnimalData; }
class Mammal : Animal { Mammal[] livesAmicablyWith; string moreMammalData; }
class Lion : Mammal { string moreLionData; }
However, I want to avoid adding members to this existing hierarchy, since they'll be at best wasted space, and at worst a bug-prone distraction.
Also, I need all of the original functionality to continue work! Here's what I was thinking of:
class AnimalExtended : Animal { }
class MammalExtended : Mammal {
public void UseLivesAmicablyWith()
{
foreach(Mammal m in livesAmicablyWith)
{
if(!(m is MammalExtended)
throw new Exception();
// use the MammalExtended
}
}
}
class LionExtended : Lion { }
Any suggestions here?