I have a problem with a program in Java. My program have an interface Bonus
and nine subclasses that extend Bonus
. I have to generate a random instance of Bonus
and I can't use prototype pattern because every bonus has a quantity that is random, so I have to create a bonus every time I need it.
Switch statement and if else is not the solution because I have too many subclasses and the code will be very long and rough.
So I decide to use reflection and it goes very well for me, but my professor say to me that reflection is a bad trick because is not type-safe and every time I change the name of bonus subclasses I have to change the code in my class GenerateBonus
.
I searched on internet and I didn't find a solution for me. So anyone know an alternative to reflection to generate object of random subclasses?