Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Last active March 23, 2018 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dammmien/52d39abbc94d0ace8006f6ace576fb5d to your computer and use it in GitHub Desktop.
Save Dammmien/52d39abbc94d0ace8006f6ace576fb5d to your computer and use it in GitHub Desktop.
JavaScript Factory design pattern
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