일대다 의존성 -> 분산 이벤트 핸들링 시스템 구현트리거구독 & 발행다음과 같은 역할이 필요하다.
구독 방법구독 리스트이벤트 발행 class Observable { //!== rxJS Observable
constructor() {
this._observers = new Set();
}
subscribe(observer) {
this._observers.add(observer);
}
unsubscribe(observer) {
this._observers.delete(observer);
}
notify(data) {
this._observers.forEach(observer => observer(data));
}
}
observer는 subscribe 메서드를 통해 this._observers(구독 리스트)에 추가
-> 이벤트 발생 시 notify

관찰 대상 : ConcreteSubject
구독자 : Observer A, B
=> 합성한 객체를 리스트로 관리, 리스트 객체에게 모두 메서드 위임을 통한 전파

: 객체에 새로운 속성을 정의하거나 이미 존재하는 속성을 수정하여 객체 반환

Object.defineProperty(object, prop, descriptor)
현대 FE 개발에서 상태관리 중요성 대두
Vue / React : 상태를 기반으로 DOM을 렌더링
but. component 깊이가 깊어져 상태관리 복잡 => Store(중앙 집중식 저장소)
// Store를 생성한다.
const store = new Store({
a: 10,
b: 20,
});
// 컴포넌트를 생성한다.
const component1 = new Component({ subscribe: [store] });
const component2 = new Component({ subscribe: [store] });
// 컴포넌트가 store를 구독한다.
component1.subscribe(store); // a + b = ${store.state.a + store.state.b}
component2.subscribe(store); // a * b = ${store.state.a * store.state.b}
// store의 state를 변경한다.
store.setState({
a: 100,
b: 200,
});
// store가 변경되었음을 알린다.
store.notify();
const store = {
state: { issueState: "open" },
listeners: [],
setState(patch) {
// TODO: state 갱신 후 notify 호출
this.state = { ...this.state, ...patch };
this.notify(this.state);
},
subscribe(fn) {
// TODO: 구독자 등록
this.listeners.push(fn);
},
notify() {
// TODO: 모든 구독자에게 state 전달
this.listeners.forEach(fn => fn(this.state));
}
};
// ---- View 객체 ----
const TextView = {
init() {
// TODO: store에 구독 등록
store.subscribe(this.render.bind(this));
},
render(state) {
if (state.issueState === "open") {
console.log("현재 상태는 OPEN 입니다.");
} else {
console.log("현재 상태는 CLOSED 입니다.");
}
}
};
// ---- Menu 뷰 객체 (완성된 코드임) ----
const Menu = {
open() {
store.setState({ issueState: "open" });
},
close() {
store.setState({ issueState: "closed" });
}
};
// ---- 연결 ----
TextView.init();
// ---- 실행 예시 ----
//Menu.close();
//Menu.open();