Challenge: Mediator Pattern
In this challenge, you have to implement the mediator pattern to solve the given problem.
We'll cover the following
Problem statement
In this challenge, you need to use the mediator pattern to implement the HR of an office that mediates the communication between the employees (workers and managers).
You have been given the dummy code for the HR
class. You need to define its constructor
and the functions:
-
registerEmployee(employee)
: Registers an employee. -
scheduleRaise(raise, worker, manager)
: Conveys the raise to the manager. Once the manager approves the raise, it gives the raise to the worker.
Now let’s look at the Manager
and Worker
classes. Both of them inherit from the Employee
class (already defined for you). You need to do the following:
-
Define the
constructor
for both classes. -
In
Manager
class, define therecieveMessage
function. It should display the message received from the HR regarding the raise of the worker. -
In
Manager
class, define theapproveRaise
function. It should display a message of approval of the raise, and return true after approval. -
In the
Worker
class, define therecieveRaise
function. It should increment the worker’s pay by the raise and display a message for the new pay.
Input
The scheduleRaise
function is called
Output
The messages conveying the raise to the manager, manager approving the raise, and the final pay are displayed
Sample input
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)
Sample output
"Joe should get 200 dollar raise" //HR conveying the message to the manager
"Joe's 200 dollar raise is approved" //manager approving the raise
"My new pay is 1600 dollars" //worker announcing the new pay
Challenge #
Take a close look and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!
class HR {//initialize the constructor hereconstructor(){//define the constructor}registerEmployee(employee){//write-code-here}scheduleRaise(raise,worker, manager){//write-code-here}}class Employee{constructor(hr,name,position,pay){this.hr = hrthis.name = namethis.position = positionthis.pay = pay}}class Manager extends Employee{//initialize the constructor hereconstructor(){//define the constructor}recieveMessage(worker,raise){//write your code here}finalizeRaise(worker,raise){//write your code here}}class Worker extends Employee{//initialize the constructor hereconstructor(){//define the constructor}recieveRaise(raise){//write your code here}}
Let’s discuss the solution in the next lesson.