I may look like a total idiot with such a question but nothing came up when I searched it, so I guess why not. Here is my problem:
I'm doing the UML work for a C++ game, that will feature animals and races.
First, I have one abstract class Entity
, and three inherited classes Human
, Dog
, and Cat
.
Now let's say I want to add a new sort of species, the Droids for example, in a way that there are three new classes: DroidHuman
, DroidDog
and DroidCat
. That's three new classes to create for one new race, and if I want to add the people of the Void, that will be three more classes to create.
Instead of doing this, I'd rather have some kind of design pattern that allows me to stick with the three original classes and only these: Human
, Dog
and Cat
.
A normal dog and a droid dog would both be instances of the Dog
class that inherits from Entity
. There would also be a Normal
super class and a Droid
super class. The difference between a normal dog and a droid dog would be that they inherit from EITHER Normal
OR Droid
.
I know I'm kind of asking the impossible but I guessed there would be a similar way to do this that I might not be aware of.
I first thought of the factory pattern, but I can't think of any way it would fix my problem.
The only other way I found to avoid this huge mess, is to add an attribute like boolean isDroid = true;
for each new race. But I am not pleased with modifying existing code to fit new stuff.
I can't be the only one to have faced that problem, am I?
Any help is appreciated. Many thanks.
Choose only one of several inheritances
--> Use Composition instead of Inheritance.