我有两个扩展 basecontroller 的控制器类。我有一个要实现的通用功能,并且在 basecontroller 中实现。
public class BaseController{
protected void populateWidget(List li, int zipcode){
//implementation for child A
populate only 10 employee records
//implementation specific to child B
populate 100 student records
}
}
public class ChildA extends BaseController{
List<Employees> li = ...
populateWidget(li, 90034)
}
public class ChildB extends BaseController{
List<Students> li = ...
populateWidget(li, 90034)
}
我已经编写了 setChildB 方法并正在研究它。像下面
public class BaseController{
protected boolean childB;
protected void setIsChildB(boolean childB){ this.childB = childB;}
protect boolean isChildB(){ return childB; }
protected void populateWidget(List li, int zipcode){
//implementation for child A
if(!isChildB())
populate only 10 employee records
else
//implementation specific to child B
populate 100 student records
}
}
public class ChildA extends BaseController{
List<Employees> li = ...
populateWidget(li, 90034)
}
public class ChildB extends BaseController{
List<Students> li = ...
setChildB(true); //setting as child B call
populateWidget(li, 90034)
}
请建议最好的方法。