为问题选择正确的模式(或根本不用)需要深入理解问题、了解模式及其权衡,并运用判断力来平衡收益与复杂性。目标是很好地解决问题,而不是为了使用模式本身而使用模式。
从问题开始,而不是模式
✓ UNDERSTAND THE PROBLEM first → what's the actual issue? (don't start by picking a pattern)
✓ Identify what you NEED → flexibility? decoupling? extensibility? simpler creation?
✓ Then ask: does a pattern address THIS problem well? (or is a simple solution better?)
→ problem-first, not pattern-first → avoid forcing patterns
将模式与问题相匹配
Recognize which patterns fit which problems:
→ need ONE instance? → Singleton (or DI/module — consider alternatives)
→ decouple object creation? → Factory; complex construction? → Builder
→ swappable algorithms / eliminate conditionals? → Strategy
→ notify many of changes? → Observer; add behavior by wrapping? → Decorator
→ incompatible interfaces? → Adapter; simplify a subsystem? → Facade
→ undo/queue actions? → Command; control access? → Proxy
→ know the patterns' INTENT (the problem each solves) to recognize the fit
