Created
August 19, 2019 19:33
-
-
Save ogaston/c0a7b539e1ff449bc4fe846041dc0b4b to your computer and use it in GitHub Desktop.
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
interface IAutomobile { | |
tires: Tire[], | |
engine: Engine | |
} | |
class Car implements IAutomobile { | |
constructor(public tires: Tire[], public engine: Engine) { } | |
} | |
class Tire { | |
constructor(public brand: string) { } | |
} | |
class Engine { | |
constructor(public cylinder: number) { } | |
} | |
class CarFactory { | |
joinTires(): Tire[] { | |
let tireSet = [] | |
for (let i = 0; i < 4; i++) { | |
tireSet.push(new Tire("Generic")) | |
} | |
return tireSet | |
} | |
createEngine(): Engine { | |
return new Engine(4) | |
} | |
buildCar(): IAutomobile { | |
const tires = this.joinTires() | |
const engine = this.createEngine() | |
return new Car(tires, engine); | |
} | |
} | |
class ElectricCar implements IAutomobile { | |
constructor(public tires: Tire[], public engine: Engine) { } | |
} | |
class ElectricEngine extends Engine { | |
constructor(batteryAh: number ) { | |
super(0) | |
} | |
} | |
class ElectricCarFactory extends CarFactory { | |
createEngine() { | |
return new ElectricEngine(3.8) | |
} | |
buildCar() { | |
const tires = this.joinTires() | |
const engine = this.createEngine() | |
return new ElectricCar(tires, engine); | |
} | |
} | |
const factory = new ElectricCarFactory() | |
const car = factory.buildCar() | |
console.log(car) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment