外觀 (Facade)

★★★★Studied

FromDesign Patterns (GoF)大話設計模式Design Patterns Explained

📚 From the Books

外觀 (Facade)ConceptStructural

給一組複雜子系統一個簡化的高層入口,讓 client 透過一道「門面」就能完成常見任務,降低與內部細節的耦合。

🧠 When to Use

TIP

訊號:要做一件事得依序戳好幾個子系統類別、記住它們的呼叫順序與相依——而大多數 client 只想要那個「常見組合」。

⚖️ Structure & Variants

經典版 — 一道門面包住啟動流程
// 子系統各自獨立、各有細節
class Cpu { fun boot() { /* ... */ } }
class Memory { fun load(addr: Long, data: ByteArray) { /* ... */ } }
class Disk { fun read(lba: Long): ByteArray = /* ... */ ByteArray(0) }

// Facade:把常見流程包成一個簡單呼叫
class ComputerFacade(
    private val cpu: Cpu = Cpu(),
    private val mem: Memory = Memory(),
    private val disk: Disk = Disk(),
) {
    fun start() {
        mem.load(0, disk.read(0))
        cpu.boot()
    }
}

ComputerFacade().start()   // client 不必知道啟動順序
  • Facade 不封死子系統:進階使用者仍可直接用 Cpu / Memory / Disk。
和 Adapter / Mediator 的差別
  • Facade:把一整組子系統收斂成一個簡化入口;單向(client → 子系統)。
  • Adapter:把一個不相容介面轉成目標介面,目的是相容。
  • Mediator:協調多個同儕物件彼此的互動,是雙向的中介。

⚠️ Misuse & Anti-patterns

🔑 Takeaways

✍️ My Notes

No notes yet — jot your takeaways or Q&A here.

📖 Further Reading