Created
July 1, 2018 19:26
-
-
Save drenther/4bc10c30a352753e2e20541f4c27e51f to your computer and use it in GitHub Desktop.
Behavioural Pattern - Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employee { | |
constructor(name, salary) { | |
this._name = name; | |
this._salary = salary; | |
} | |
work() { | |
return `${this._name} handles ${this.responsibilities() /* gap to be filled by subclass */}`; | |
} | |
getPaid() { | |
return `${this._name} got paid ${this._salary}`; | |
} | |
} | |
class Developer extends Employee { | |
constructor(name, salary) { | |
super(name, salary); | |
} | |
// details handled by subclass | |
responsibilities() { | |
return 'application development'; | |
} | |
} | |
class Tester extends Employee { | |
constructor(name, salary) { | |
super(name, salary); | |
} | |
// details handled by subclass | |
responsibilities() { | |
return 'testing'; | |
} | |
} | |
// usage | |
const dev = new Developer('Nathan', 100000); | |
console.log(dev.getPaid()); // 'Nathan got paid 100000' | |
console.log(dev.work()); // 'Nathan handles application development' | |
const tester = new Tester('Brian', 90000); | |
console.log(tester.getPaid()); // 'Brian got paid 90000' | |
console.log(tester.work()); // 'Brian handles testing' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment