lundi 4 mai 2015

Nice way of factory method pattern with inheritance

Suppose I have following class hierarchy:

class abstract Parent{}

class FirstChild extends Parent {}

class SecondChild extends Parent {}

And I'd like to create DTO objects from each child:

class abstract ParentDTO {}

class FirstChildDTO extends ParentDTO{}

class SecondChildDTO extends ParentDTO{}

I think I need a factory method something like this:

ParentDTO createFrom(Parent source); 

Is there any nice way to do this without instanceof checks and if/else statements?

Aucun commentaire:

Enregistrer un commentaire