refactoring : large class - extract superclass

This commit is contained in:
haerong22
2022-04-11 14:09:20 +09:00
parent d690d7c0e1
commit ef6ebca959
5 changed files with 121 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package com.example.refactoring._20_large_class._41_extract_superclass;
import java.util.List;
public class Department extends Party {
private List<Employee> staff;
public Department(String name) {
super(name);
}
@Override
public double monthlyCost() {
return this.staff.stream().mapToDouble(Employee::monthlyCost).sum();
}
public List<Employee> getStaff() {
return staff;
}
public int headCount() {
return this.staff.size();
}
}

View File

@@ -0,0 +1,21 @@
package com.example.refactoring._20_large_class._41_extract_superclass;
public class Employee extends Party {
private Integer id;
private double monthlyCost;
public Employee(String name) {
super(name);
}
@Override
public double monthlyCost() {
return monthlyCost;
}
public Integer getId() {
return id;
}
}

View File

@@ -0,0 +1,19 @@
package com.example.refactoring._20_large_class._41_extract_superclass;
public abstract class Party {
protected String name;
public Party(String name) {
this.name = name;
}
public String getName() {
return name;
}
public double annualCost() {
return this.monthlyCost() * 12;
}
public abstract double monthlyCost();
}

View File

@@ -0,0 +1,30 @@
package com.example.refactoring._20_large_class._41_extract_superclass._before;
import java.util.List;
public class Department {
private String name;
private List<Employee> staff;
public String getName() {
return name;
}
public List<Employee> getStaff() {
return staff;
}
public double totalMonthlyCost() {
return this.staff.stream().mapToDouble(e -> e.getMonthlyCost()).sum();
}
public double totalAnnualCost() {
return this.totalMonthlyCost() * 12;
}
public int headCount() {
return this.staff.size();
}
}

View File

@@ -0,0 +1,26 @@
package com.example.refactoring._20_large_class._41_extract_superclass._before;
public class Employee {
private Integer id;
private String name;
private double monthlyCost;
public double annualCost() {
return this.monthlyCost * 12;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public double getMonthlyCost() {
return monthlyCost;
}
}