Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Created December 20, 2020 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpluta/77012dc8ff3a1fdfd94b4f3f6212d9dd to your computer and use it in GitHub Desktop.
Save pawelpluta/77012dc8ff3a1fdfd94b4f3f6212d9dd to your computer and use it in GitHub Desktop.
Article: Law of Demeter - company using internal methods
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