デザインパターンは事前に設計されるのではなく、リファクタリングを通じて出現することがよくあります。コードが成長し、その必要性が明確になるにつれて、パターンに向かってリファクタリングすることで設計が改善されます。これは、推測ではなく実際に必要とされたときにパターンを適用するという原則を反映しています。
パターンはリファクタリングを通じて出現する
Rather than designing patterns in UPFRONT (often premature/speculative), patterns often
emerge as you REFACTOR existing code:
→ start SIMPLE → as needs become clear (real complexity, real duplication, real change
points), REFACTOR toward a pattern that addresses them
→ "refactor TO a pattern" when the code would genuinely benefit
→ patterns as a destination of refactoring, not a starting blueprint
パターンへのリファクタリング例
→ A growing if/else or switch on "type" → refactor to STRATEGY or polymorphism
→ Duplicated object creation logic → extract a FACTORY
→ A class doing too much (God object) → split responsibilities (toward SRP; extract classes)
→ Hardcoded dependencies (hard to test) → introduce DEPENDENCY INJECTION
→ Complex subsystem usage scattered around → introduce a FACADE
→ Adding behavior via subclass explosion → refactor to DECORATOR (composition)
