Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 진행기록
- 학습내용정리
- version management
- mixin pattern
- 모던 자바스크립트 Deep Dive
- 이벤트
- 블로그 서비스 최적화
- 자바스크립트 딥다이브
- unique identifiers
- const
- 딥다이브
- 커리어
- 새 코드 받아오기
- 이미지 갤러리 최적화
- DOM
- package management
- peerdependencies
- 스코프
- 올림픽 통계 서비스 최적화
- pr review
- js pattern
- 자바스크립트 패턴
- 제너레이터와 async/await
- 자바스크립트
- 브라우저의 렌더링 과정
- js pattern
- Babel과 Webpack
- 프로그래머스
- middleware pattern
- 프론트엔드 성능 최적화 가이드
Archives
- Today
- Total
Dev Blog
Observer pattern 본문
With the observer pattern,
we can subscribe certain objects, the observers, to another object, called the observable.
Whenever an event occurs, the observable notifies all its observers!
- observers: an array of observers that will get notified whenever a specific event occurs
An observable object usually contains 3 important parts:
- subscribe(): a method in order to add observers to the observers list
- unsubscribe(): a method in order to remove observers from the observers list
- notify(): a method to notify all observers whenever a specific event occurs
Code Example
// observable.js
class Observable {
constructor() {
this.observers = [];
}
subscribe(f) {
this.observers.push(f);
}
unsubscribe(f) {
this.observers = this.observers.filter(subscriber => subscriber !== f);
}
notify(data) {
// function 들이 Data 를 이용하여 실행된다.
this.observers.forEach(observer => observer(data));
}
}
export default new Observable();
// App.js
import React from "react";
import { Button, Switch, FormControlLabel } from "@material-ui/core";
import { ToastContainer, toast } from "react-toastify";
import observable from "./Observable";
function handleClick() {
observable.notify("User clicked button!"); // 값은 data
}
function handleToggle() {
observable.notify("User toggled switch!"); // 값은 data
}
function logger(data) {
console.log(`${Date.now()} ${data}`);
}
function toastify(data) {
toast(data, {
position: toast.POSITION.BOTTOM_RIGHT,
closeButton: false,
autoClose: 2000
});
}
observable.subscribe(logger); // logger 함수를 구독에 추가
observable.subscribe(toastify); // toastify 함수를 구독에 추가
export default function App() {
return (
<div className="App">
<Button variant="contained" onClick={handleClick}>
Click me!
</Button>
<FormControlLabel
control={<Switch name="" onChange={handleToggle} />}
label="Toggle me!"
/>
<ToastContainer />
</div>
);
}
Pros
- Using the observer pattern is a great way to enforce separation of concerns and the single-responsiblity principle.
- The observer objects aren’t tightly coupled to the observable object, and can be (de)coupled at any time.
- The observable object is responsible for monitoring the events, while the observers simply handle the received data.
Cons
If an observer becomes too complex, it may cause performance issues when notifying all subscribers.
'Development Notes > JS Patterns' 카테고리의 다른 글
Module pattern (0) | 2024.12.08 |
---|---|
Mixin pattern (0) | 2024.12.08 |
Middleware Pattern (0) | 2024.12.08 |
Mediator Pattern (0) | 2024.12.08 |
Flyweight Pattern (0) | 2024.12.08 |
Comments