interface IMessageService {
sendMessage(): void
}
// a service that
// implements IMessageService
class EmailService implements IMessageService {
sendMessage(): void {
console.log("Email sent!")
}
}
// another service that
// implements IMessageService
class SMSService implements IMessageService {
sendMessage(): void {
console.log("SMS sent!")
}
}
// client service that depends on
// an instance of IMessageService
// through constructor injection
class NotifierService {
constructor(private message: IMessageService) {}
sendMessage(): void {
this.message.sendMessage()
}
}
// assembler/injection code
// manual wiring
const email = new EmailService()
const emailNotifier = new NotifierService(email)
const sms = new SMSService()
const smsNotifier = new NotifierService(sms)
for (const notifier of [emailNotifier, smsNotifier]) {
notifier.sendMessage()
}
To embed this project on your website, copy the following code and paste it into your website's HTML: