Article: Law of Demeter - company using internal methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Company { | |
private List<Department> departments; | |
Map<DepartmentCode, BigDecimal> costPerDepartment() { | |
Map<DepartmentCode, BigDecimal> costPerDepartment = new HashMap<>(); | |
departments.forEach(department -> { | |
DepartmentCode departmentCode = department.getCode(); | |
BigDecimal employeesCost = calculateCostOf(department); | |
costPerDepartment.put(departmentCode, employeesCost); | |
}); | |
return costPerDepartment; | |
} | |
private BigDecimal calculateCostOf(Department department) { | |
return department.getTeams().stream() | |
.map(team -> calculateCostOf(team)) | |
.reduce(BigDecimal.ZERO, BigDecimal::add); | |
} | |
private BigDecimal calculateCostOf(Team team) { | |
return team.getMembers().stream() | |
.map(Member::getCost) | |
.reduce(BigDecimal.ZERO, BigDecimal::add); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment