pattern-factory-method

Refactor domain object creation into factory methods with validation.

Updated Mar 26, 2026
One-click install
npx skills add https://github.com/johnnystefan/test-saas-business --skill pattern-factory-method
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pattern-factory-method
Source: https://github.com/johnnystefan/test-saas-business/tree/main/skills/design-patterns/creationals/factory-method
Command: npx skills add https://github.com/johnnystefan/test-saas-business --skill pattern-factory-method

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill teaches the Factory Method pattern to separate concrete class instantiation from clients, preventing rigid dependencies when domain objects like BusinessUnit or Membership require validation before creation.

Core Features & Use Cases

  • Direct instantiation detection: Highlights spots with new ConcreteClass() calls that should be routed through a creator.
  • Product interface definition: Ensures all products implement a shared contract so callers can remain agnostic to concrete types.
  • Concrete creators: Builds subclasses that override the factory method to return specific domain entities while encapsulating construction logic.

Quick Start

Ask the skill to refactor Membership.create() so the domain entity is produced through a Factory Method that validates invariants first.

Frequently Asked Questions about pattern-factory-method

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I decouple domain object creation from direct class instantiation in TypeScript?

Decouple domain object creation by abstracting instantiation through factory methods, replacing direct `new ConcreteClass()` calls with creator subclasses that validate invariants before returning concrete domain entities.

When should I use the Factory Method pattern for refactoring domain services?

Use the Factory Method pattern when refactoring domain service initialization or extensible entity construction requiring subclass-specific instantiation logic and polymorphic contracts to prevent rigid dependencies.

How to refactor entity creation to validate invariants before returning concrete domain objects?

Refactor entity creation by building concrete creators that override a factory method to return specific domain entities, encapsulating construction logic and validating invariants before the product interface returns the object.

Does the Factory Method pattern require product interfaces for polymorphic contracts?

Yes, the Factory Method pattern requires product interfaces with polymorphic contracts so callers remain agnostic to concrete types, ensuring all products implement a shared contract validated by creators before instantiation.

What is the best way to handle extensible entity construction requiring subclass-specific instantiation logic?

The best way to handle extensible entity construction is applying the Factory Method pattern, using concrete creators to override instantiation logic and validate invariants while maintaining a shared product interface for polymorphic access.

Why does direct instantiation cause tight coupling in domain-driven design?

Direct instantiation causes tight coupling in domain-driven design because clients depend rigidly on concrete classes, preventing flexible validation of invariants or subclass-specific instantiation logic before domain entities like BusinessUnit are created.