Challenge: Solution Review

This lesson will explain the solution to the problem from the last 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.approveRaise(worker,raise)
if(ans){
worker.receiveRaise(raise)
}
}
}
class Employee{
constructor(hr,name,position,pay){
this.hr = hr
this.name = name
this.position = position
this.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`)
}
approveRaise(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 += raise
console.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 scheduleRaise function:

scheduleRaise(raise,worker, manager){
    manager.receiveMessage(worker,raise)
    var ans = manager.finalizeRaise(worker,raise)
    if(ans){
      worker.receiveRaise(raise)
    }
}

First, the manager will receive the raise message from the HR through the receiveMessage function. Next, HR will wait for the manager to approve the raise. The approveRaise will return true when the manager authorizes the raise. Once, the raise is approved, the worker will receive the payment through the receiveRaise function.

Now let’s look at the Employee class:

class Employee{
  constructor(hr,name,position,pay){
    this.hr = hr
    this.name = name
    this.position = position
    this.pay = pay 
  }
}

An employee object has the properties: name, position, pay, and the hr that the employee communicates with.

The Manager class extends the Employee class.

class Manager extends Employee{
    constructor(hr,name,position,pay){
      super(hr,name,position,pay)
      this.hr.registerEmployee(this)
    }
   //code...
}

The constructor invokes super to initialize all the properties of the Employee class for the Manager. Whenever a manager instance is created, it gets registered into the company by the hr. You can see this in the following line in the constructor:

this.hr.registerEmployee(this)

The Manager class also defines the receiveMessage function.

receiveMessage(worker,raise){
     console.log(`${worker.name} should get ${raise} dollar raise`)
}

This function displays the message the Manager receives from the HR regarding the raise of the worker.

approveRaise function is also defined in the Manager class.

approveRaise(worker,raise){
    console.log(`${worker.name}'s ${raise} dollar raise is approved`)
      return true
}

This function displays the message regarding the manager approving the raise of the worker. It returns true at the end to convey that the raise has been approved.

The Worker class also extends the Employee class.

class Worker extends Employee{
  constructor(hr,name,position,pay){
      super(hr,name,position,pay)
      this.hr.registerEmployee(this)
  }
  //code...
}

The constructor invokes super to invoke all the properties of the Employee class for the Worker. Whenever a worker instance is created, it gets registered into the company by the hr. You can see this in the following line in the constructor:

this.hr.registerEmployee(this)

The Manager class also defines the receiveRaise function.

receiveRaise(raise){
    this.pay += raise
    console.log(`My new pay is ${this.pay} dollars`)
}

This function increments the pay of the worker by the raise. Then it displays the message announcing the new pay of the worker.


Let’s discuss the observer pattern in the next lesson.