SOLID는 다음 5가지 원칙의 앞 글자를 따서 부르는 이름이다.
SRP(Single Responsiblity Principle) : 단일 책임 원칙
OCP(Open Closed Principle) : 개방 폐쇄 원칙
LSP(Liskov Substitution Principle) : 리스코프 치환 원칙
ISP(Interface Segregation Principle) : 인터페이스 분리 원칙
DIP(Dependency Inversion Principle) : 의존 역전 원칙
이 중에서 개방 폐쇄 원칙에 대해서 작성해보려고 한다.
개방 폐쇄 원칙(OCP: Open Closed Principle)
“소프트웨어 엔티티(클래스, 모듈, 함수 등)는 확장에 대해서는 열려 있어야 하지만 변경에 대해서는 닫혀 있어야 한다.”
-로버트 C. 마틴-
출처: https://ittrue.tistory.com/544 [IT is True:티스토리]
type Send = {
send: (title: string, content: string) => void
}
type Props = {
title: string,
content: string,
} & Send
const 알림발송_기능 = ({send, title, content}:Props) => {
// 알림 발송 기능을 구현
// 1. 네이버 발송, 2. 카카오 발송, 3. 트위터 발송등 어떤 알림인지 알 수가 없지만 발송해야 함
send(title, content);
}
type KaKao = {
} & Send
class KaKaoSend implements KaKao {
constructor(private apiKey: string) {
}
send(title: string, content: string): void {
// kakaoLib.kakaoSendMessage(title, content);
}
}
// 장점 : test코드 할 때
알림발송_기능('', '', new KaKaoSend('sdkjfklsdjfk'));
알림발송_기능('', '', new Naver('sdkjfklsdjfk'));