Observer Pattern

patternsdev/skills/javascript/observer-pattern

作者 patternsdev48bf58a488cdMIT253 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 個月前更新

Teaches the observer pattern for event-driven communication. Use when you need decoupled publish/subscribe behavior where multiple parts of your system react to state changes or events.

AI 產生的概覽

講解觀察者模式,用於 JavaScript 與 TypeScript 中解耦的發布/訂閱事件通訊。

功能
此技能講解觀察者模式:觀察者物件訂閱可觀察物件,當事件發生時由可觀察物件通知它們。它示範如何建立帶有 subscribe、unsubscribe 與 notify 方法的 Observable 類別,並提供完整範例,讓 logger 與 toastify 函式回應按鈕點擊與開關切換。它也說明何時適合或不適合使用此模式,以及其優缺點。
適用情境
當應用程式的多個部分需要得知狀態變更或事件時,或需要在元件之間實作事件驅動的非同步通訊時,可使用它。在權衡解耦與通知效能、除錯複雜度時也適用。
執行需求
不需要指令碼或特殊工具,僅為說明性內容。範例涉及 JavaScript/TypeScript 以及 React、react-toastify、RxJS 等函式庫,但閱讀這些指引無需安裝任何東西。

Observer Pattern

Table of Contents

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!

When to Use

  • Use this when you need to notify multiple parts of an application about state changes or events
  • This is helpful for implementing event-driven, asynchronous communication between components

When NOT to Use

  • When the subscriber count is very high and notification performance becomes critical
  • When simpler callbacks or direct function calls suffice for one-to-one communication
  • When debugging difficulty from implicit event chains outweighs the decoupling benefit

Instructions

  • Create an Observable class with subscribe, unsubscribe, and notify methods
  • Keep observers loosely coupled to the observable for better separation of concerns
  • Be mindful of performance when notifying many subscribers with complex logic
  • Consider using libraries like RxJS for more advanced reactive programming needs

Details

An observable object usually contains 3 important parts:

  • observers: an array of observers that will get notified whenever a specific event occurs
  • 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

Let's create an observable using an ES6 class:

js
class Observable {  constructor() {    this.observers = [];  }
  subscribe(func) {    this.observers.push(func);  }
  unsubscribe(func) {    this.observers = this.observers.filter((observer) => observer !== func);  }
  notify(data) {    this.observers.forEach((observer) => observer(data));  }}

We can now add observers to the list of observers with the subscribe method, remove the observers with the unsubscribe method, and notify all subscribers with the notify method.

Let's build something with this observable. We have a very basic app that only consists of two components: a Button, and a Switch.

js
export default function App() {  return (    <div className="App">      <Button>Click me!</Button>      <FormControlLabel control={<Switch />} />    </div>  );}

We want to keep track of the user interaction with the application. Whenever a user either clicks the button or toggles the switch, we want to log this event with the timestamp. Besides logging it, we also want to create a toast notification that shows up whenever an event occurs!

Whenever the user invokes the handleClick or handleToggle function, the functions invoke the notify method on the observer. The notify method notifies all subscribers with the data that was passed by the handleClick or handleToggle function!

First, let's create the logger and toastify functions. These functions will eventually receive data from the notify method.

js
import { ToastContainer, toast } from "react-toastify";
function logger(data) {  console.log(`${Date.now()} ${data}`);}
function toastify(data) {  toast(data);}
export default function App() {  return (    <div className="App">      <Button>Click me!</Button>      <FormControlLabel control={<Switch />} />      <ToastContainer />    </div>  );}

Currently, the logger and toastify functions are unaware of observable: the observable can't notify them yet! In order to make them observers, we'd have to subscribe them, using the subscribe method on the observable!

js
import { ToastContainer, toast } from "react-toastify";
function logger(data) {  console.log(`${Date.now()} ${data}`);}
function toastify(data) {  toast(data);}
observable.subscribe(logger);observable.subscribe(toastify);
export default function App() {  return (    <div className="App">      <Button>Click me!</Button>      <FormControlLabel control={<Switch />} />      <ToastContainer />    </div>  );}

Whenever an event occurs, the logger and toastify functions will get notified. Now we just need to implement the functions that actually notify the observable: the handleClick and handleToggle functions! These functions should invoke the notify method on the observable, and pass the data that the observers should receive.

js
import { ToastContainer, toast } from "react-toastify";
function logger(data) {  console.log(`${Date.now()} ${data}`);}
function toastify(data) {  toast(data);}
observable.subscribe(logger);observable.subscribe(toastify);
export default function App() {  function handleClick() {    observable.notify("User clicked button!");  }
  function handleToggle() {    observable.notify("User toggled switch!");  }
  return (    <div className="App">      <Button>Click me!</Button>      <FormControlLabel control={<Switch />} />      <ToastContainer />    </div>  );}

We just finished the entire flow: handleClick and handleToggle invoke the notify method on the observer with the data, after which the observer notifies the subscribers: the logger and toastify functions in this case.

Whenever a user interacts with either of the components, both the logger and the toastify functions will get notified with the data that we passed to the notify method!

Although we can use the observer pattern in many ways, it can be very useful when working with asynchronous, event-based data. Maybe you want certain components to get notified whenever certain data has finished downloading, or whenever users sent new messages to a message board and all other members should get notified.

Case study

A popular library that uses the observable pattern is RxJS. RxJS has tons of built-in features and examples that work with the observable pattern.

Pros

Using the observer pattern is a great way to enforce separation of concerns and the single-responsibility 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.

Source

References

來源與署名

來源:patternsdev/skills位於javascript/observer-pattern提交48bf58a

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架