You are on page 1of 2

We have a class BadEmployee which deals with multiple functionalities

(responsabilities). Adding new methods will modify the class without a good
reason.

public class BadEmployee {

private String status;


private String name;
private int hours;

public BadEmployee(String status, String name, int hours) {


this.status = status;
this.name = name;
this.hours = hours;
}

public int calculatePay() {


switch (this.status) {
case "A":
return 1;
case "B":
return 2;
default:
return 0;
}
}

public String reportHours() {


return String.format("%s worked %d hours.\n", this.name, this.hours);
}

@Override
public String toString() {
return "I am a employee";
}
}

Let’s refactor, so that the responsibility of the class GoodEmplyee is to define an employee, and the
other functionalities are delegated to dedicated services.
public class GoodEmployee {

String status;
String name;
int hours;

public GoodEmployee(String status, String name, int hours) {


this.status = status;
this.name = name;
this.hours = hours;
}
}

public class EmployeePaymentService {

private EmployeeAdditionalPaymentService eAPS;

public EmployeePaymentService(EmployeeAdditionalPaymentService
employeeAdditionalPaymentService) {
this.eAPS = employeeAdditionalPaymentService;
}

public int calculatePay(GoodEmployee employee) {


switch (employee.status) {
case "A":
return 1;
case "B":
return 2;
default:
return 0;
}
}
}

public class EmployeeTimetrackingService {

public String reportHours(GoodEmployee employee) {


return String.format("%s worked %d hours.\n", employee.name,
employee.hours);
}

You might also like