sept08
sept08

Reputation: 345

How to fix a feature-envy of project based on Java?

After I used "inFusion Hydrogen" to analyze my project, it's pretty easy to figure out myQuestionFun() is causing feature-envy.

public abstract class Father{
    protected DataModel dataModel;
    public abstract void myQuestionFun();
}
public class Child extends Father{

    public DataModel<TableInfo> tableList = new DataModel<TableInfo>;
    public DataModel<TableInfo> getTableList(){
        return this.tableList;
    }

    @Override
    public DataModel<Report> getDataModel(){
        return this.dataModel;
    }

    @Override
    public void myQuestionFun(){
        List<TableInfo> tabList = new ArrayList<TableInfo>();
        for (int i=0; dataModel.getDataList().size(); i++) {
            Report rep = (Report)dataModel.getDataList.get(i);
            TableInfo tabInfo = new TableInfo();
            tabInfo.setId(rep.getId());
            tabInfo.setName(rep.getName());
            tabList.add(tabInfo);
        }
        tableList.setPage(dataModel.getPage());
        tableList.setSorter(dataModel.getSorter());
   }
}

The question is, what's the best way to fix it? Because myQuestionFun() is a function of the abstract method of the parent class, I can't move it.

Upvotes: 2

Views: 157

Answers (2)

Ajay Deshwal
Ajay Deshwal

Reputation: 1306

Extract following code as a method from Child into the Father:

protected List<TableInfo> getTabList() {
    final List<TableInfo> tabList = new ArrayList<TableInfo>();
    for (int i=0; dataModel.getDataList().size(); i++) {
            Report rep = (Report) dataModel.getDataList.get(i);
            TableInfo tabInfo = new TableInfo();
            tabInfo.setId(rep.getId());
            tabInfo.setName(rep.getName());
            tabList.add(tabInfo);
    }
    return tabList;
}

Move following method to Father, if it is ALWAYS supposed to return this.dataModel :

public DataModel<Report> getDataModel(){
   return this.dataModel;
}

Upvotes: 1

noscreenname
noscreenname

Reputation: 3370

Remove dataModel field form Father and change in into an interface.

Upvotes: 0

Related Questions