This is a simple scenario in an office with employees and their manager.
In this scenario, both managers and employees have a name attribute.
- Only the manager can change his own name
- Only the employee can change his own name
Only the employee has a designation and a workStat attribute. The work status indicates whether the employee is still working (true) or not working (false).
A manager can fire any selected employee using the Manager.fireEmployee() method.
For the above scenario, I created the following class diagram:
I implemented the above scenario using Java as shown below. (Orchestration code is not included)
class Manager{ private String name; //create Manager object by providing name Manager(String managerName ){ this.name = managerName; } //only manager can change his name private void changeName(String managerName){ this.name = managerName; } //manager can fire any selected Employee public void fireEmployee(Employee emp){ emp.changeWorkStat(false); } } /* --------------------------- */ class Employee{ private String name; private String designation; private boolean workStat; Employee(String name ,String designation ){ this.designation = designation; this.name = name; this.workStat = true; } //only Employee can change his name private void changeName(String empName){ this.name = empName; } //to fire the employee manager can execute this method public void changeWorkStat(boolean stat){ this.workStat = stat; } } But i have doubts. Is it good OOAD practice to include the fireEmployee() method inside the manager class? Because this method is basically just executing the method of another class. Also, this method is not responsible for managing the state of the Manager class (doing nothing to attributes in the Manager class, not using the attributes in the Manager class). Therefore, it is a sign of violation of encapsulation.
If this is not good OOAD practice, then what is the correct way of implement this scenario in code?
