Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // define facory as a meta-class
- class Employee {
- create (type) {
- let employee
- if (type === 'fulltime') {
- employee = new FullTime()
- } else if (type === 'parttime') {
- employee = new PartTime()
- } else if (type === 'temporary') {
- employee = new Temporary()
- } else if (type === 'contractor') {
- employee = new Contractor()
- }
- employee.type = type
- employee.say = function () {
- console.log(`${this.type}: rate ${this.rate}/hour`)
- }
- }
- }
- // define classes for every job type
- class Fulltime {
- constructor () {
- this.rate = '$12'
- }
- }
- class PartTime {
- constructor () {
- this.rate = '$11'
- }
- }
- class Temporary {
- constructor () {
- this.rate = '$10'
- }
- }
- class Contractor {
- constructor () {
- this.rate = '$15'
- }
- }
- // usage example
- // create new factory
- const factory = new Employee()
- fulltime = factory.create('fulltime')
- parttime = factory.create('parttime')
- temporary = factory.create('temporary')
- contractor = factory.create('contractor')
- // use it
- fulltime.say()
- parttime.say()
- temporary.say()
- contractor.say()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement