Challenge: Solution Review
This lesson will explain the solution to the problem from the previous coding challenge.
We'll cover the following...
Solution #
Press + to interact
class HR {constructor(){this.employeeList = []}registerEmployee(employee){this.employeeList[employee.name] = employee}scheduleRaise(raise,worker, manager){manager.receiveMessage(worker,raise)var ans = manager.finalizeRaise(worker,raise)if(ans){worker. receiveRaise(raise)}}}class Employee{constructor(hr,name,position,pay){this.hr = hrthis.name = namethis.position = positionthis.pay = pay}}class Manager extends Employee{constructor(hr,name,position,pay){super(hr,name,position,pay)this.hr.registerEmployee(this)}receiveMessage(worker,raise){console.log(`${worker.name} should get ${raise} dollar raise`)}finalizeRaise(worker,raise){console.log(`${worker.name}'s ${raise} dollar raise is approved`)return true}}class Worker extends Employee{constructor(hr,name,position,pay){super(hr,name,position,pay)this.hr.registerEmployee(this)}receiveRaise(raise){this.pay += raiseconsole.log(`My new pay is ${this.pay} dollars`)}}var hr = new HR()var employee = new Worker(hr,"Joe","Developer",1400)var manager = new Manager(hr,"Allen","Team Lead",3000)hr.scheduleRaise(200,employee,manager)
Explanation
In this challenge, you had to use the mediator pattern to implement the HR of an office. HR acts as a communication channel between different employees of a company, such as a manager and the workers.
Let’s start by discussing the HR
class.
class HR {
constructor(){
this.employeeList = []
}
//code...
}
The constructor
initializes an array employeeList
that will contain all the employees in the company.
Next, it has the registerEmployee
function that registers an employee by adding them to the list of employees.
registerEmployee(employee){
this.employeeList[employee.name] = employee
}
Next, it defines the ...