Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.