# 外觀模式 - Facade

![](https://2481295995-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lejh-llXQZynxNlmsNC%2F-Lejh0DCzMnypN9tYlMe%2F-Lejh240xpiMueAXPqPk%2Ffacade1.jpg?generation=1557725991094274\&alt=media)

外觀模式在複雜的商業邏輯上提供了簡單的介面。如果直接把商業邏輯的所有介面直接暴露給使用者，使用者需要單獨面對這一大堆複雜的介面，學習成本很高，而且存在誤用的隱患。如果使用外觀模式，我們只要暴露必要的 API 就可以了。

下圖演示了外觀模式的基本概念：

![](https://2481295995-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lejh-llXQZynxNlmsNC%2F-Lejh0DCzMnypN9tYlMe%2F-Lejh2426OvMkkIlRNTC%2Ffacade2.png?generation=1557725990974549\&alt=media)

API 的使用者完全不知道這內部的商業邏輯有多麼複雜。當我們有大量的類別並且它們使用起來很複雜而且也很難理解的時候，外觀模式是一個十分理想的選擇。

外觀模式把使用和背後的實現邏輯成功解耦，同時也降低了外部程式碼對內部工作的依賴程度。如果底層的類別發生了改變，外觀的介面並不需要做修改。

舉個例子，如果有一天你想換掉所有的後台服務，你只需要修改 API 內部的程式碼，外部調用 API 的程式碼並不會有改動。


---

# 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/facade.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.
