refactoring : large class - extract superclass
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user