Skip to content

Instantly share code, notes, and snippets.

@AliN11
Created September 15, 2019 12:58
Show Gist options
  • Save AliN11/1e4647c3902666b9dcadbf346f4b8655 to your computer and use it in GitHub Desktop.
Save AliN11/1e4647c3902666b9dcadbf346f4b8655 to your computer and use it in GitHub Desktop.
Typescript Dependency Injection
interface Wheel {}
interface Engine {}
class Car {
private wheel: Wheel;
private engine: Engine;
public constructor(wheel: Wheel, engine: Engine) {
this.wheel = wheel;
this.engine = engine;
}
}
class AdvancedWheel implements Wheel {
// ...
}
class AdvancedEngine implements Engine {
// ...
}
class EconomicWheel implements Wheel {
// ...
}
class EconomicEngine implements Engine {
// ...
}
// Client
let advWheel = new AdvancedWheel();
let advEngine = new AdvancedEngine();
let advancedCar = new Car(advWheel, advEngine);
let ecoWheel = new EconomicWheel();
let ecoEngine = new EconomicEngine();
let economicCar = new Car(ecoWheel, ecoEngine);
@mostafa8722
Copy link

mostafa8722 commented Oct 18, 2022

it was a simple and useful example ):

@AmirHosein-Zare
Copy link

thank you for your useful exmaple ...

@ShimaMasoomi
Copy link

very simple and useful, thank you

@Mirshirin
Copy link

excellent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment