> For the complete documentation index, see [llms.txt](https://wilden-chen.gitbook.io/swift-design-patterns/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wilden-chen.gitbook.io/swift-design-patterns/master.md).

# 實作 Swift 設計模式

說到設計模式，相信大家都不陌生，但是又有多少人知道它背後的真正含義？絕大多數程式設計師都知道設計模式十分重要，不過關於這個話題的文章卻不是很多，開發者們在開發的時候有時也不太在意設計模式方面的內容。

設計模式針對應用程式設計中的常見問題，提供了一些靈活的解決方案，開發者可以通過這些程式設計的方式寫出易於理解且能夠重複應用的程式碼。正確的使用設計模式可以降低程式碼之間的耦合度，很輕鬆的修改或者替換以前的程式碼。

大家對設計模式這名詞雖然感覺還很陌生，其實在我們現在做過的各種專案，其實你在不知不覺已經用了很多設計模式。這得益於 Cocoa 提供的框架和一些良好的程式撰寫的習慣。接下來的這篇教程將會帶你一起領略設計模式的魅力。

> 📖 如果你是 Swift 的學習者，也歡迎參考作者的另一本書：[**Swift 殿堂之路**](https://wilden-chen.gitbook.io/swift-bethel-of-the-road)

## 常見模式

第一部分我們將會完成一個完整的應用，展示 Flickr 推薦照片 的瀏覽器應用程式。

通過開發這個應用程式，我們會接觸一些 Cocoa 中常見的設計模式：

* 創建型 (Creational)：單例模式 (Singleton)、簡易工廠模式(Simple Factory)
* 結構型 (Structural)：裝飾者模式 (Decorator)、適配器模式 (Adapter)、外觀模式 (Facade)、MVC、MVP
* 行為型 (Behavioral)：觀察者模式 (Observer)、事件流模式 (Event Flow)

先來預覽一下最終的結果：

![](/files/-Lejh1PhvuSYmh4Pipkl)

我們將會完成一個完整的應用，展示 Flickr 推薦照片 的瀏覽器應用程式。

通過開發這個應用程式，我們會接觸一些 Cocoa 中常見的設計模式：

* 創建型 (Creational)：單例模式 (Singleton)、簡易工廠模式(Simple Factory)
* 結構型 (Structural)：裝飾者模式 (Decorator)、適配器模式 (Adapter)、外觀模式 (Facade)、MVC、MVP
* 行為型 (Behavioral)：觀察者模式 (Observer)、事件流模式 (Event Flow)

### 聲明

本書初稿可追溯至 2015 年前後的 Swift 學習筆記與實作整理，並於 2019 年整理為 GitBook 版本。2026 年版本則在保留原始學習脈絡的基礎上，使用 AI 工具協助進行格式轉換、章節整理、文字潤飾與部分範例補充；最終內容仍由作者檢視、取捨與發布。

本書的教學內容經過改編後，並不適合一開始剛接觸 iOS 開發的新手，而是已經熟悉基本 Swift 撰寫開發以外，並且對於物件導向 OOP 已經有基本概念的開發者進一步學習使用。

1. 已有 OOP 概念。
2. 已熟悉 Swift 程式語言。
3. 已瞭解 Project、Library、WorkSpace 以及 cocoaPods 使用方式。

本書範例以 **Swift 5.x / Xcode 15+** 為基準撰寫與測試。

### AI 協作整理說明

本書原始內容、章節設計與教學脈絡為作者既有作品。AI 僅作為編輯與整理協作工具，不作為本書作者；內容取捨、責任與最終發布仍由 Wilden Chen 負責。

### 本書改編自：

* [Swift设计模式 (iOS)](https://www.gitbook.com/book/yourtion/swiftdesignpatterns/details)
* [iOS 中的設計模式 (Swift版本) Part 1](http://blog.callmewhy.com/2014/12/29/introducing-ios-design-patterns-in-swift-part-1/)
* [iOS 中的設計模式 (Swift版本) Part 2](http://blog.callmewhy.com/2015/03/01/introducing-ios-design-patterns-in-swift-part-2/)
* [大話設計模式](http://www.books.com.tw/products/0010430101)

### 版權與授權

除上列改編與引用來源另有其原始權利歸屬外，本書文字整理、教學編排、範例說明與本版本內容由 Wilden Chen 保留著作權。未經作者同意，請勿將本書內容整本轉載、重製、販售或改作為商業教材。

讀者可以為個人學習、課堂討論或非商業技術交流引用本書片段；引用時請標示作者與來源連結。若要進行較大篇幅引用、翻譯、改編、出版或商業使用，請先取得作者同意。

文中提及的 Swift、iOS、Xcode、Apple、Flickr 等名稱、商標與第三方服務，權利歸其各自權利人所有。本書所附範例與外部連結以原始發布時狀態為基準，日後可能因平台、語言版本或服務調整而需要自行更新。

### 完成檔案

[範例完整版](https://github.com/wildenchen/swift-bethel-of-the-road/tree/eed3c8ff6c1141681b3d3b2c69fac406cd11c24c/DesignPatterns/workspaces/FlickrPhotos-Finish.zip)

***

## 本書目錄

* [入門 - 開始](/swift-design-patterns/gettingstarted.md)
* [設計模式之王 - MVC](/swift-design-patterns/mvc.md)
* [如何使用 MVC 模式](/swift-design-patterns/mvc/use-mvc.md)
* [簡易工廠模式 - Simple Factory](/swift-design-patterns/simplefactory.md)
* [如何使用 簡易工廠模式](/swift-design-patterns/simplefactory/use-simplefactory.md)
* [外觀模式 - Facade](/swift-design-patterns/facade.md)
* [如何使用外觀模式](/swift-design-patterns/facade/use-facade.md)
* [裝飾者模式 - Decorator](/swift-design-patterns/decorator.md)
  * [裝飾者 - 擴展](/swift-design-patterns/decorator/decorator-extension.md)
  * [如何使用擴展](/swift-design-patterns/decorator/use-decorator-extension.md)
  * [裝飾者 - 委派](/swift-design-patterns/decorator/decorator-delegation.md)
  * [如何使用委派模式](/swift-design-patterns/decorator/use-decorator-delegation.md)
* [單例模式 - Singleton](/swift-design-patterns/singleton.md)
* [如何使用單例模式](/swift-design-patterns/singleton/use-singleton.md)
* [適配器模式 - Adapter](/swift-design-patterns/adapter.md)
* [如何使用適配器模式](/swift-design-patterns/adapter/use-adapter.md)
* [觀察者模式 - Observer](/swift-design-patterns/observer.md)
  * [通知 - Notification](/swift-design-patterns/observer/notification.md)
  * [鍵值觀察 - KVO](/swift-design-patterns/observer/kvo.md)
  * [事件流 - Event Flow](/swift-design-patterns/observer/eventflow.md)
* [設計模式之神話 - MVP](/swift-design-patterns/mvp.md)
* [如何使用 MVP 模式](/swift-design-patterns/mvp/use-mvp.md)
* [最後的潤色](/swift-design-patterns/mvp/finaltouches.md)
* [本章小結](/swift-design-patterns/final.md)

***

**作者：Wilden Chen**

* Email：<wilden0424@gmail.com>
* 原始 GitBook 編寫：約 2019 年
* 2026 年版本整理：Wilden Chen 與 AI 協作完成格式同步與說明補充


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
