策略 (Strategy)

★★★★★Studied

FromDesign Patterns (GoF)大話設計模式Agile Principles, Patterns, and Practices

📚 From the Books

策略 (Strategy)ConceptBehavioral

把一族「可以互換的演算法」各自封裝成獨立物件,讓使用端在執行期挑一個用。核心是把「會變的那一步」抽成介面,用組合注入,而不是寫死成 if-else。

🧠 When to Use

TIP

訊號:同一件事有很多種「做法」要在執行期切換(排序規則、計費方式、壓縮演算法、付款管道),而且你正想寫一個大 when

⚖️ Structure & Variants

經典版 — 介面 + 多個實作,組合注入
fun interface PricingStrategy { fun price(base: Int): Int }

val regular = PricingStrategy { it }
val member  = PricingStrategy { (it * 0.9).toInt() }
val vip     = PricingStrategy { (it * 0.8).toInt() }

class Checkout(private val pricing: PricingStrategy) {   // ← 注入策略
    fun total(base: Int) = pricing.price(base)
}

Checkout(vip).total(1000)   // 800
  • Checkout 不知道有幾種定價、也不含任何 if;換策略 = 換注入物件。
函數式版 — 策略就是一個函數
  • 當策略沒有狀態,介面只有一個方法時,直接傳一個 lambda / function 就是策略模式(如上 fun interface)。
  • 這也是為什麼在 FP 裡「策略模式」幾乎隱形——高階函數本來就在做這件事。
和 State 的差別(結構幾乎一樣)
  • Strategy:由外部選定一個演算法,物件存活期內通常不自己換。
  • State:狀態物件自己決定下一個狀態,行為隨內部狀態流轉而改變。
  • 結構同形,差在「誰決定切換、切換語意是不是狀態機」。

⚠️ Misuse & Anti-patterns

🔑 Takeaways

✍️ My Notes

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

📖 Further Reading