Let's say I have a class named Human
in ProjectA
. It is instantiated in the CreatureBuilder
class of the same project.
Now I want to create a new class called Cyborg
in a different project, i.e. in ProjectB
. ProjectB
has ProjectA
in its imports, but ProjectA
knows nothing about ProjectB
.
Cyborg extends Human
, and must also be instantiated by CreatureBuilder
of ProjectA
(so, Cyborg
is located in ProjectB
, I call CreatureBuilder
from ProjectB
to instantiate Cyborg
, but CreatureBuilder
is located in ProjectA
, as well as my Human
class).
I need a logic to create a Human
when CreatureBuilder
is instantiated from ProjectA
, and to create a Cyborg
when CreatureBuilder
is instantiated from ProjectB
.
I know that it can be done by creating an interface with a getCreature()
method (that will be overridden in ProjectB
) and a factory class. But can I use Reflection API instead? From its description, looks like it was designed to do something related (please correct me if I'm wrong). I know that reflections are slow though, and I normally avoid them. But it would be great to see a proof-of-concept, just out of curiosity.
Aucun commentaire:
Enregistrer un commentaire