gof-refactor-to-pattern
Use when refactoring existing code toward a known GoF pattern — phrases like "refactor this to use Strategy", "extract a Decorator from these flags", "this switch should be State", "replace conditional with polymorphism", "introduce a Factory here". Sequences small behavior-preserving refactor steps and links to the destination pattern skill.
How do I install this agent skill?
npx skills add https://github.com/yonatankarp/software-design-skills --skill gof-refactor-to-patternIs this agent skill safe to install?
- Gen Agent Trust Hubpass
No security issues detected. This skill is purely instructional and provides guidance for refactoring code toward design patterns without any high-risk operations.
- Socketpass
No alerts
- Snykpass
Risk: LOW · No issues
What does this agent skill do?
One-line summary
Take existing code and step-by-step move it toward a target GoF pattern — small commits, behavior preserved at every step, tests green between each.
When to use this skill
- The user knows the destination pattern and wants the path.
- A code smell has a known pattern-shaped fix (big switch on enum → State; primitive flag explosion → Decorator).
- Migration from procedural / transaction-script code to OO with patterns.
When NOT to use this skill
- The user doesn't know which pattern fits — route to
gof-identify. - The user wants a review, not a refactor — route to
gof-review. - The user wants the pattern in new code → route to
gof-implementstyle (here, that maps togof-kotlin-idiomsfor direct code).
Core content
General discipline: never combine a refactor with a feature change in one commit. Each refactor step must keep behavior identical and pass tests; if there are no tests, write characterization tests first.
Common refactors and their step sequences:
Big switch on type code → Strategy.
- Identify the varying behavior (the method body inside each
switchcase). - Extract each branch into its own object implementing a common
Strategyinterface. - Replace the switch with a map /
whenthat selects the strategy. - Inline the lookup until clients hold strategy references directly.
Subclass-per-variant → Strategy or Composition.
- Identify the single dimension that varies between subclasses.
- Extract that dimension as a Strategy interface.
- Collapse the subclasses back into one class that composes a Strategy.
Conditional behavior accumulating in one class → Decorator.
- Identify the orthogonal extensions hiding in
if (hasX)checks. - Extract each extension into a Decorator class wrapping the base.
- Compose decorators in the construction site instead of toggling flags.
Status enum + switch in many methods → State.
- Draw the state diagram explicitly.
- Create a State class per state.
- Move the per-state behavior from each
switchinto the corresponding State. - The context delegates to the current State; states drive transitions by setting
context.state = NextState.
Multiple new ConcreteX() decisions across client code → Factory.
- Centralize the choice in one method (Simple Factory).
- If the choice depends on a subclass's identity, promote to Factory Method.
- If matched families must be created together, promote to Abstract Factory.
Direct calls between many subsystems → Facade.
- Identify the common sequences callers use.
- Define a Facade class whose methods are the high-level intents.
- Migrate callers to the Facade; keep the subsystem accessible to power users.
Static getInstance() everywhere → Dependency Injection.
- Identify the Singleton's callers.
- Add a constructor parameter (or field) for the dependency.
- Wire the dependency once at the application's composition root.
- Delete
getInstance().
Decision heuristics
- Each refactor step is small, reversible, and committable.
- Run tests between each step. If tests don't exist, write characterization tests first (record-and-replay style).
- Refactor and feature changes never share a commit.
- If the refactor balloons, split it into multiple PRs — refactor first, then feature.
Anti-patterns
- Big-bang rewrites in the name of "now we'll do it right".
- Refactoring without tests.
- Renaming classes to pattern names without restructuring.
- Refactor + feature in the same commit — review and revert become hard.
See also
- All thirteen pattern skills (the destinations).
gof-identify— when destination is unknown.gof-review— to catch smells worth refactoring.ddd-refactor— the DDD-flavored counterpart.
References
- Head First Design Patterns (2nd ed), Ch. 13 (patterns in the real world).
- Joshua Kerievsky, Refactoring to Patterns (2004) — book-length treatment of the same idea; complements this skill.
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/yonatankarp/software-design-skills/gof-refactor-to-pattern">View gof-refactor-to-pattern on skillZs</a>