옵저버 패턴을 사용해 Subject 클래스를 구현하세요.
옵저버 패턴은 한 객체(Subject)의 상태 변화나 이벤트를 등록된 옵저버들에게 알리는 일대다(one-to-many) 의존 관계를 정의합니다. 발행자와 구독자가 느슨하게 결합되어 있어 시스템의 유연성을 높입니다.
subscribe(observer): 옵저버를 구독 목록에 추가합니다unsubscribe(observer): 옵저버를 구독 목록에서 제거합니다 (해당 참조만)notify(event): 모든 구독 중인 옵저버의 update(event)를 호출합니다update(event) 메서드를 가진 객체입니다const subject = new Subject();
const received: string[] = [];
const observer = { update: (e: string) => received.push(e) };
subject.subscribe(observer);
subject.notify('hello'); // observer.update('hello') 호출
subject.unsubscribe(observer);
subject.notify('bye'); // 호출되지 않음