design-pattern-factory-method
Use when object creation varies by context and you need to extend product types without rewriting client orchestration code.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Skill: Factory Method
Intent
Define a creation hook in a base creator and let concrete creators decide which concrete product to instantiate. This separates product-selection volatility from stable client workflows.
Applicability Signals
- Signal 1: Constructors are selected through repeated
if/elseorswitchbranches across multiple call sites. - Signal 2: New product variants appear regularly and force edits in existing orchestrator code.
- Signal 3: Different deployment/runtime contexts require different product implementations behind one shared interface.
Contraindications
- Case 1: There is only one product implementation and no expected variant growth.
- Case 2: Product differences are trivial configuration values, not distinct behavior.
- Case 3: Team cannot sustain extra creator subclasses and resulting class-count overhead.
Decision Heuristics
- If product families are likely to expand while usage flow should remain stable, prefer Factory Method.
- If creation rules are static and centralized, a plain constructor or simple factory function is usually enough.
- Decision anti-bias note: do not introduce subclass hierarchies just to look “pattern-complete.”
Implementation Checklist
- Define product interface/contract and concrete products.
- Define creator abstraction with factory method signature.
- Implement concrete creators that override the factory method.
- Keep client logic dependent on creator/product abstractions only.
- Add tests proving new creator/product pairs require no client changes.
Misuse Checks
- Misuse 1: Creator subclasses only differ by one literal value → Remediation: collapse to parameterized constructor/factory function.
- Misuse 2: Client still checks concrete product types after creation → Remediation: move behavior to product interface methods.
- Misuse 3: Factory method starts returning unrelated product contracts → Remediation: tighten product interface and split creators by responsibility.
Verification Rubric
- Correctness:
- Primary creator returns expected product variant for baseline context.
- New creator/product extension works without changes to client orchestration.
- Design quality:
- Creator and product roles are explicit, minimal, and cohesive.
- Concrete types are isolated from client dependency boundaries.
- Regression safety:
- Tests cover baseline creation path and one extension path.
Language-Specific Adaptations (Optional)
- TypeScript: use abstract classes or interface-driven creators with discriminated return typing where needed.
- Python: prefer ABC protocols for product contracts; factory method can be instance or class-level based on context.
- Go: model creator/product through interfaces; keep constructor functions private to packages when possible.
Related Patterns (Optional)
- Abstract Factory: choose when you need coordinated creation of multiple related product types.
- Template Method: combine when creator workflow is fixed but one creation step varies.
Attribution & Sources
- Source Site: Refactoring.Guru
- Source URLs:
- Derivation Note: Concepts derived from referenced sources; explanatory wording rewritten for this repository.
- Policy Note: This artifact intentionally includes no direct quotes and no Refactoring.Guru images.
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-03-11
- 索引更新时间
- 2026-03-11
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!