λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ“  archive

[Design Pattern] PUB-SUB νŒ¨ν„΄

by HandHand 2022. 6. 13.

πŸ“Œ PUB-SUB νŒ¨ν„΄

PUB-SUB νŒ¨ν„΄μ€ Observer νŒ¨ν„΄μ˜ 일쒅이며

λ‹€μˆ˜μ˜ consumer κ°€ μžμ‹ μ΄ 관심 μžˆλŠ” 이벀트λ₯Ό λΉ„λ™κΈ°μ μœΌλ‘œ μˆ˜μ‹ ν•  수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

(단, sender 와 receiver λŠ” coupling λ˜μ§€ μ•Šμ•„ μ„œλ‘œμ˜ 쑴재λ₯Ό λͺ¨λ¦…λ‹ˆλ‹€.)

 

πŸ“Œ PUB-SUB vs Observer

Observer Pattern λ™μž‘ 흐름

 

μœ„λŠ” Observer Pattern 의 λ™μž‘ νλ¦„μž…λ‹ˆλ‹€.

μ΄λŸ¬ν•œ 흐름을 κ΅¬ν˜„ν•œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ€ Observer Pattern 을 κ΅¬ν˜„ν–ˆλ‹€κ³  말할 수 μžˆμŠ΅λ‹ˆλ‹€.

 

κ·Έλ ‡λ‹€λ©΄ PUB-SUB λŠ” μ–΄λ–€ 차이가 μžˆλŠ” 것 μΌκΉŒμš”?

PUB-SUB 의 경우 Subject -> Publisher 그리고 Observer -> Subscriber λΌλŠ”

λͺ…μΉ­μƒμ˜ λ³€κ²½λ§Œ μ‘΄μž¬ν•  뿐 λˆˆμ— λ„λŠ” 차이점은 μ—†μ–΄ λ³΄μž…λ‹ˆλ‹€.

κ·Έλž˜μ„œ λ§Žμ€ μ„œμ λ“€μ΄ Observer === PUB-SUB 라고 ν•˜λŠ” κ²½μš°κ°€ μ‘΄μž¬ν•˜λŠ” 것이죠. 🀨

 

κ·Έλž˜μ„œ Observer 와 PUB-SUB 의 차이점이 λ­”κ°€μš”?

두 νŒ¨ν„΄μ˜ μž₯점이자 곡톡점은 두 객체 κ°„μ˜ μƒν˜Έ μ˜μ‘΄μ„±μ„ λŠμŠ¨ν•˜κ²Œ ν•΄μ€€λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

Observer Pattern κ³Ό PUB-SUB Pattern 의 μ•½κ°„μ˜ 차이점은 λ°”λ‘œ
Topic/Event channel 의 쑴재 μœ λ¬΄μž…λ‹ˆλ‹€.

 

 

이둜써 Publisher λŠ” Subscriber 에 직접 λ©”μ‹œμ§€λ₯Ό 보내지 μ•ŠμœΌλ©° 이 λ‘˜μ€ μ„œλ‘œμ˜ 쑴재λ₯Ό μ•Œμ§€ λͺ»ν•©λ‹ˆλ‹€.

Event Broker ν˜Ήμ€ Event Bus 라고 ν•˜λŠ” 것이 이 λ‘˜μ˜ 쀑간 닀리 역할을 ν•˜μ—¬ λ©”μ‹œμ§€λ₯Ό 필터링 ν•΄μ€λ‹ˆλ‹€.

 

λ©”μ‹œμ§€ ν•„ν„°λ§μ—λŠ” Topic-based & Content-based κ°€ μ‘΄μž¬ν•˜λŠ”λ°
이 λ‘˜μ˜ 차이점은 λ‹€μŒ λΈ”λ‘œκ·Έ 글을 μ°Έκ³ ν•˜λ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.

 

Publish-subscribe pattern

 

Publish–subscribe pattern - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Messaging pattern in which senders and receivers do not directly communicate In software architecture, publish–subscribe is a messaging pattern where senders of messages, called publ

en.wikipedia.org

 

πŸ“Œ μ™œ ν•„μš”ν• κΉŒ?

μ—¬λŸ¬ μ»΄ν¬λ„ŒνŠΈκ°€ κ³΅ν†΅λœ 데이터λ₯Ό μ €μž₯ν•˜λŠ” μ‹œλ‚˜λ¦¬μ˜€μ—μ„œ

κΉ”λ”ν•˜κ³  멋진 μ½”λ“œλ₯Ό μœ„ν•΄μ„œλŠ” κ΄€μ‹¬μ‚¬μ˜ 뢄리 κ°€ ν•„μš”ν•©λ‹ˆλ‹€.

 

μ„œλΉ„μŠ€ νŒŒμΌμ€ 둜컬 μŠ€ν† λ¦¬μ§€μ™€ 동기화λ₯Ό ν•˜λŠ” λ“±μ˜ λ©”μ»€λ‹ˆμ¦˜κ³Ό 관심사λ₯Ό 닀루고 λ‚˜λ¨Έμ§€ μ»΄ν¬λ„ŒνŠΈλŠ” UI 에 μ§‘μ€‘ν•œλ‹€.

 

ꡬ독/λ°œν–‰μ„ 톡해 μ›ν•˜λŠ” μ‹œμ μ΄ λ“±λ‘λœ ν•¨μˆ˜κ°€ 호좜될 수 μžˆλ„λ‘ ν•œλ‹€.

 

μš°λ¦¬κ°€ μ§€κΈˆκ» μ‚¬μš©ν•œ λ‹€μ–‘ν•œ API 듀은 Publish & Subscribe νŒ¨ν„΄κ³Ό μœ μ‚¬ν•œ λ™μž‘ 원리λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€.

 

addEventListener νŠΉμ • μ΄λ²€νŠΈμ— ν•¨μˆ˜λ₯Ό κ΅¬λ… ν•˜κ³ 
DOM μš”μ†Œμ— μ˜ν•΄ μ•‘μ…˜μ΄ λ°œν–‰λ˜λ©΄ ν•΄λ‹Ή ν•¨μˆ˜κ°€ 호좜
Promise νŠΉμ • μ•‘μ…˜λ“€μ΄ μ™„λ£Œλ˜λŠ” 것을 κ΅¬λ…ν•˜λ©°
데이터가 μ€€λΉ„λ˜λ©΄ λ°œν–‰λ˜λŠ” νŒ¨ν„΄
React 의 state와 props μ»΄ν¬λ„ŒνŠΈλ“€μ΄ 데이터 λ³€ν™”λ₯Ό κ΅¬λ…ν•˜μ—¬ μ—…λ°μ΄νŠΈ
Fetch API λ§ˆμ°¬κ°€μ§€ μž…λ‹ˆλ‹€ πŸ˜„

 

즉, μ–΄λ– ν•œ 비동기 처리 방법을 μ‚¬μš©ν•˜λ“ μ§€ μ–Έμ œλ‚˜ 같은 νŒ¨ν„΄! (λ¬΄μ–Έκ°€λŠ” κ΅¬λ…ν•˜κ³  λ¬΄μ–Έκ°€λŠ” λ°œν–‰)

λ˜ν•œ Loosing Coupling 은 각 λͺ¨λ“ˆμ΄ μ„œλ‘œμ˜ μΈν„°νŽ˜μ΄μŠ€μ— μ˜μ‘΄ν•˜μ§€ μ•Šμ•„ λͺ¨λ“ˆμ˜ μž¬μ‚¬μš©μ„±μ΄ 증가 ν•©λ‹ˆλ‹€.

 

πŸ“Œ 단점은 μ—†μ„κΉŒ?

Observer Pattern μ΄λ‚˜ PUB-SUB Pattern λͺ¨λ‘ consumer 와 receiver κ°„μ˜ λŠμŠ¨ν•œ μ—°κ²° λ•Œλ¬Έμ—

μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ™μž‘ μ‹œ crash κ°€ λ°œμƒν–ˆμ„ λ•Œ μ‹œμŠ€ν…œμ˜ λ‹€λ₯Έ 뢀뢄에 영ν–₯을 주지 μ•ŠλŠ”λ‹€λŠ” 것을 보μž₯ν•˜κ±°λ‚˜

좔적 및 λ””λ²„κΉ…ν•˜κΈ° νž˜λ“€ 수 μžˆμŠ΅λ‹ˆλ‹€. (μ‹€μ œλ‘œ κ²ͺμ—ˆμŠ΅λ‹ˆλ‹€... 😭)

πŸ’‘ μ£Όμ˜ν•˜κΈ°
λ•Œλ‘œλŠ” 읽기 쒋은 μ½”λ“œμ™€ μœ μ§€ 보수λ₯Ό μœ„ν•΄ 응집도λ₯Ό λ†’μ΄λŠ” 것이 더 μœ λ¦¬ν•  수 μžˆμœΌλ―€λ‘œ
PUB-SUB Pattern 을 λ‚¨μš©ν•˜μ§€ μ•Šλ„λ‘ μ£Όμ˜ν•©μ‹œλ‹€

 

πŸ“Œ PUB-SUB λ₯Ό κ΅¬ν˜„ν•œ JS 라이브러리 및 μ˜ˆμ‹œ

Modern Web Best Practice: Pub/Sub | Modern Web

 

Modern Web Best Practice: Pub/Sub | Modern Web

Postal.js and Amplify are both really good libraries for implementing publisher / subscriber architecture in your application, so which should you use?

modernweb.com

Publisher subscriber pattern in React.js

 

Publisher subscriber pattern in React.js

Publisher subscriber pattern is a design pattern which is similar to observer pattern. In this blog, we will learn how to implement it in react without using any third-party library.

dev.to

 

πŸ“Œ 참고자료

Applying publisher subscriber design pattern in javascript

Observer vs Pub-Sub pattern | Hacker Noon

[λ²ˆμ—­] 초보 ν”„λ‘ νŠΈμ—”λ“œ κ°œλ°œμžλ“€μ„ μœ„ν•œ Pub-Sub(Publish-Subscribe) νŒ¨ν„΄μ„ μ•Œμ•„λ³΄κΈ°

Why would one use the Publish/Subscribe pattern (in JS/jQuery)?

The Publisher/Subscriber Pattern in JavaScript

JavaScript Design Patterns Part 2: The Publisher/Subscriber Pattern

 

λ°˜μ‘ν˜•

πŸ’¬ λŒ“κΈ€