Last active
March 23, 2018 23:06
-
-
Save Dammmien/52d39abbc94d0ace8006f6ace576fb5d to your computer and use it in GitHub Desktop.
JavaScript Factory design pattern
This file contains hidden or 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 Car { | |
constructor(size, price, maxSpeed) { | |
this.size = size; | |
this.price = price; | |
this.maxSpeed = maxSpeed; | |
} | |
} | |
class CarFactory { | |
create(size) { | |
if (size === 'small') return new Car(size, 10800, 130); | |
if (size === 'medium') return new Car(size, 15200, 150); | |
if (size === 'large') return new Car(size, 20300, 170); | |
} | |
} | |
const carFactory = new CarFactory(); | |
carFactory.create('medium'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment