About gof-refactor-to-pattern
A skill for safely refactoring existing code toward a known Gang-of-Four pattern through small, test-verified steps. It is for developers who know the target pattern and need a behavior-preserving migration path.
Key features
- 01Stepwise pattern refactoring
- 02Behavior-preserving changes
- 03Test-first guidance
- 04Sequences for common pattern migrations
- 05Refactor and feature work kept separate
Use cases
- 01Replacing a large type switch with Strategy
- 02Moving conditional extensions into Decorator objects
- 03Converting status-dependent logic to State
- 04Replacing repeated singleton access with dependency injection
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ yonatankarp/ software-design-skills --skill gof-refactor-to-patternAfter installing, type/gof-refactor-to-patternin Claude Code to confirm it loaded.
Details
From the project READMEOne-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.