我有一个实体列表。每个实体都继承一些基类。在控制器中,我应该确定每个实体对象的相应视图。我该怎么做?
class BaseEntity {}
class FooEntity extends BaseEntity {}
class BarEntity extends BaseEntity {}
class View {}
class FooView extends View {}
class BarView extends View {}
控制器代码:
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。适当的OOP溶液是优选的。
答案 0 :(得分:1)
使用ViewFactory根据entity
的类型创建相应的类型视图:
BaseEntity entities = repository.getSomeEntities();
List<View> views = new ArrayList();
foreach(BaseEntity entity in entities)
{
View view = viewFactory.Create(entity);
views.add(view);
}