Inheritance
I'm trying to create a setup where the data and logic of my weapons are separate. Hence there being a Weapon class, and WeaponData.
We have many types of weapons, that also inherit deeply. Think Pistol inherits from SingleShotWeapon which inherits from FireArm which inherits from Weapon. Usually each type also has a separate config class, WeaponData_Pistol.
I'm struggling to find a way to nicely have this sub-class defined in the class itself. The deep inheritance chain makes generics difficult, i cant just add a generic field and constraint to Weapon and end that chain at any point since it will be inherited even further.
It also means we need to construct our class like this: `new FireArm<FireArmData>, which is a bit odd. It should always use a FireArmData.
8 Replies
This new obviously wont work, the base classes wont be assigned
Generics but i dont like that much either. There is no final class, so i always have to define the T just to pass it through. Its odd to have to pass this type in the pistol constructor
my first thouguht would be use less inheritance
Yeah i agree but a) legacy and b) there is some shared code in all of them.
But yes, we wouldve loved to separate these in more components and use composition instead.
for example SingleShotWeapon to me it doesn't look like it should be part of the identity of the weapon
it's a property
i'm not sure i would go even deeper than FireArm
i could maybe think of composing the inherited FireArm classes with interfaces (it depends on how variable items are), but not inheriting them from Weapon
i would avoid this at all costs
it would probably be unusable
Thanks for your response
in the end it depends on how you use this stuff, for sure
Enjoy my crappy composition approach. I've actually tried something similar to what you have up there with generics, which turned into covariance, which turned into me digging a gigantic hole.