Created
September 15, 2019 12:58
-
-
Save AliN11/1e4647c3902666b9dcadbf346f4b8655 to your computer and use it in GitHub Desktop.
Typescript Dependency Injection
This file contains 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 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very simple and useful, thank you