Feature Envy

When a class wants to access way too much of another class's internals - it wants to be a part of that other class and get all of its data. Sometimes, this is unavoidable howerver in order to avoid tight internal coupling and provide proper abstraction.

public class HourlyEmployeeReport {
    private HourlyEmployee employee ;
    public HourlyEmployeeReport(HourlyEmployee e) {
        this.employee = e;
    }
    String reportHours() {
        return String.format(
            "Name: %s\tHours:%d.%1d\n",
            employee.getName(),
            employee.getTenthsWorked()/10,
            employee.getTenthsWorked()%10);
    }
}

Here the reportHours function "envies" the data inside of employee; however we don't want to expose how the employee report is formatted (string.format) to the employee, who doesn't care about this information. Thus we will leave it as is.



bj 2019-09-22