mercredi 27 juillet 2016

How to determine a view for the polymorphic model in MVC

I have a list of entities. Each entity inherits some base class. In the controller I should determine a corresponding view for each entity object. How should I do that?

class BaseEntity {}
class FooEntity extends BaseEntity {}
class BarEntity extends BaseEntity {}

class View {}
class FooView extends View {}
class BarView extends View {}

Controller code:

BaseEntitty entities = repository.getSomeEntities();
List<View> views = new ArrayList();
for(BaseEntity in entities) {
    View view; //How can I determine the concrete view class?
    views.add(view);
}

P.S. A proper OOP solution is preferable.

Aucun commentaire:

Enregistrer un commentaire