Created
February 20, 2021 18:57
-
-
Save nchDev-js/56f24d0f50b738295c650fa950292fdd to your computer and use it in GitHub Desktop.
Design Patterns / CREATIONAL
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
| 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!"; | |
| } | |
| } |
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() { | |
| 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; | |
| } | |
| } |
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 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); | |
| } | |
| } |
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 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); | |
| } | |
| } |
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 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