SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Car {
  2.   start(typeEngine) {
  3.     const engine = this.createEngine(typeEngine);
  4.  
  5.     engine.start();
  6.   }
  7.  
  8.   createEngine(typeEngine) {
  9.     throw new Error('You have to implement the method createEngine!');
  10.   }
  11. }
  12.  
  13. class MyCar extends Car {
  14.   createEngine(typeEngine) {
  15.     let engine;
  16.  
  17.     if (typeEngine === 'Jet') {
  18.       engine = new Jet();
  19.     } else if (typeEngine === 'Gas') {
  20.       engine = new Gas();
  21.     } else {
  22.       engine = new Brayton();
  23.     }
  24.  
  25.     return engine;
  26.   }
  27. }
  28.  
  29. class CarFactory {
  30.   createCar() {
  31.     throw new Error('You have to implement the method createCar!');
  32.   }
  33. }
  34.  
  35. class JetCarFactory extends CarFactory {
  36.   createCar() {
  37.     return new MyCar('Jet');
  38.   }
  39. }
  40.  
  41. class GasCarFactory extends CarFactory {
  42.   createCar() {
  43.     return new MyCar('Gas');
  44.   }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top