jeudi 30 juillet 2020

What way would be best to adhere to both DRY and limit unintended functionality given this kind of design in C# or Java?

Please excuse my limited paint skills, but how would I best create objects that would function in the way depicted in this diagram?, given the single-class inheritance nature of the languages? The Moon, Rocky Planet, and Asteroid classes should implement IColonizable, but the issue is that they would implement it in the exact same way, say, with a simple getter. In this case, I would assume it would be better to put the functionality into the parent class Body. However, I would then be giving the GasGiant class the ability to getOwner() when I do not want this functionality to exist.

The best solution I can come up with is implementing getOwner in the Body class, but only inheriting the IColonizable interface in the desired classes, then maybe when getOwner() is called in GasGiant, I could override with an exception. Any thoughts?

Aucun commentaire:

Enregistrer un commentaire