SOLID : 종속성 반전
그래서 의존성 주입에 대한 내 자신의 코드를 사용할 것입니다. 아직 이 글을 읽지 않으셨다면 먼저 이 글을 읽어주세요

"😭 미친 남친 탈출!" 종속성 주입을 가장 간단한 방법으로 설명
Kaziu ・ 9월 4일 ・ 1분 읽기
#javascript
#beginners
#programming
▼ 의존성 주입에 대한 이 기사의 잘못된 코드
class Woman {
makeLover(){
const bf = new CrazyMan()
bf.stayWith()
}
}
class CrazyMan {
stayWith() {
console.log('I am dangerous man, stay with me')
}
}
const woman = new Woman()
woman.makeLover()
이미지는 이렇습니다. 여자는 미친 남자에게 달려있다

이 방향을 반대로 해야 하는데 어떻게?
인터페이스를 사용하여 해결합니다. 이것이 종속성 반전입니다.
class Woman {
constructor(man: IMan) {
this.man = man
}
makeLover(){
this.man.stayWith()
}
}
// ⭐⭐ this interface is key !!!!!!!
interface IMan {
stayWith: () => void
}
class CrazyMan {
stayWith() {
console.log('I am dangerous man, stay with me')
}
}
class GoodMan {
stayWith() {
console.log('I am good man, stay with me')
}
}
const man = new GoodMan()
const woman = new Woman(man)
woman.makeLover()
// I am good man, stay with me
▼ 이제 Crazy man, Good man은 Man Interface에 의존합니다. 이제 방향이 반전되는 것을 볼 수 있습니다.

심판
https://khalilstemmler.com/articles/tutorials/dependency-injection-inversion-explained/
This is good sample of Typescript, it's in japanese though
Reference
이 문제에 관하여(SOLID : 종속성 반전), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/kaziusan/solid-dependency-inversion-399h텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)