Skip to content

Instantly share code, notes, and snippets.

@nchDev-js
Created February 20, 2021 18:57
Show Gist options
  • Select an option

  • Save nchDev-js/56f24d0f50b738295c650fa950292fdd to your computer and use it in GitHub Desktop.

Select an option

Save nchDev-js/56f24d0f50b738295c650fa950292fdd to your computer and use it in GitHub Desktop.
Design Patterns / CREATIONAL
function bmwProducer(kind) {
return kind === "sport" ? sportCarFactory : familyCarFactory;
}
function sportCarFactory() {
return new Z4();
}
function familyCarFactory() {
return new I3();
}
class Z4 {
info() {
return "Z4 is a Sport car!";
}
}
class I3 {
info() {
return "i3 is a Family car!";
}
}
class Car {
constructor() {
this.autoPilot = false;
this.parktronic = false;
this.signaling = false;
}
}
class CarBuilder {
constructor() {
this.car = new Car();
}
addAutoPilot(autoPilot) {
this.car.autoPilot = autoPilot;
return this;
}
addParktronic(parktronic) {
this.car.parktronic = parktronic;
return this;
}
addSignaling(signaling) {
this.car.signaling = signaling;
return this;
}
updateEngine(engine) {
this.car.engine = engine;
return this;
}
build() {
return this.car;
}
}
class Bmw {
constructor(model, price, maxSpeed) {
this.model = model;
this.price = price;
this.maxSpeed = maxSpeed;
}
}
class BmwFactory {
create(type) {
if (type === "X5") return new Bmw(type, 108000, 300);
if (type === "X6") return new Bmw(type, 111000, 320);
}
}
class TeslaCar {
constructor(model, price, interior, autopilot) {
this.model = model;
this.price = price;
this.interior = interior;
this.autopilot = autopilot;
}
produce() {
return new TeslaCar(this.model, this.price, this.interior, this.autopilot);
}
}
class Counter {
constructor() {
if (typeof Counter.instance === "object") {
return Counter.instance;
}
this.count = 0;
Counter.instance = this;
return this;
}
getCount() {
return this.count;
}
increaseCount() {
return this.count++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment