策略模式(Strategy Pattern):一种常见的软件设计模式,把一组可互换的算法/行为封装成独立的“策略”对象,并通过统一接口在运行时自由切换,从而避免大量 if/else 或 switch,提高可扩展性与可维护性。(除软件语境外,pattern 也可泛指“模式/规律”,但此短语最常指设计模式。)
/ˈstrætədʒi ˈpætərn/
We used the Strategy Pattern to switch payment methods without changing the checkout code.
我们使用策略模式在不修改结账代码的情况下切换支付方式。
By applying the Strategy Pattern, the team isolated complex pricing rules into separate strategies, making the system easier to test and extend.
通过应用策略模式,团队将复杂的定价规则拆分到不同策略中,使系统更易测试与扩展。
strategy 源自希腊语 strategia(“将军的指挥/用兵之道”),引申为“策略”。pattern 来自古法语 patron(“范本、样式”),后发展为“模式”。“Strategy Pattern”作为术语主要流行于面向对象设计领域,因《Design Patterns》(GoF,“四人帮”)一书对其系统化命名与传播而广为人知。