# 觀察者模式 - Observer

在觀察者模式裡，一個物件在狀態變化的時候會通知另一個物件。參與者並不需要知道其他物件的具體是幹什麼的 - 這是一種降低耦合度的設計。這個設計模式常用於在某個屬性改變的時候通知關注該屬性的物件。

常見的使用方法是觀察者註冊監聽，然後再狀態改變的時候，所有觀察者們都會收到通知。

在 MVC 裡，觀察者模式意味著需要允許 `Model` 對象和 `View` 對象進行交流，而不能有直接的關聯。

基於 `Cocoa` 或是 `Cocoa Touch` 下的觀察者模式有下列兩個：

* 通知：`Notification`
* 鍵值觀察：`Key-Value Observing (KVO)`

而其他程式語言，如 Java 或是 C# 還有一種觀察者模式，稱之為事件（Event Flow），而在 iOS 開發裡面，我們使用下面這個 Framework 來實踐。

* 事件流：`LionEvents`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wilden-chen.gitbook.io/swift-design-patterns/observer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
