Skip to content

Instantly share code, notes, and snippets.

@ogaston
Created August 19, 2019 19:33
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 ogaston/c0a7b539e1ff449bc4fe846041dc0b4b to your computer and use it in GitHub Desktop.
Save ogaston/c0a7b539e1ff449bc4fe846041dc0b4b to your computer and use it in GitHub Desktop.
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