设计模式是针对常见软件设计问题的可重用、经过验证的解决方案 —— 这些是构造代码以解决重复出现的问题的通用模板。它们提供了共享的词汇和经过测试的方法,有助于编写更好、更易维护的代码。
什么是设计模式
text
DESIGN PATTERNS = proven, reusable solutions to RECURRING design problems:
→ not code to copy, but TEMPLATES/approaches for structuring code
→ distilled from experience (best practices for common situations)
→ e.g. "how do I ensure only one instance of a class?" → Singleton pattern
→ describe the PROBLEM, the SOLUTION structure, and the trade-offs
经典分类(四人帮)
text
CREATIONAL → how OBJECTS are created (Singleton, Factory, Builder, Prototype)
STRUCTURAL → how objects/classes are COMPOSED (Adapter, Decorator, Facade, Proxy, Composite)
BEHAVIORAL → how objects INTERACT/communicate (Observer, Strategy, Command, Iterator, State)
→ the famous "GoF" (Gang of Four) patterns; many others exist too
