Design Patterns Analyzer Skill
Purpose: Detect, suggest, and evaluate Gang of Four (GoF) design patterns in TypeScript/JavaScript codebases with stack-aware adaptations.
Core Capabilities
- Stack Detection: Identify primary framework/library (React, Next.js, MapLibre, Prisma)
- Pattern Detection: Find existing implementations of 23 GoF patterns
- Smart Suggestions: Recommend patterns to fix code smells, using stack-native idioms when available
- Quality Evaluation: Assess pattern implementation quality against best practices
StarMapper Stack Context
- Primary: React 18 + Next.js App Router + TypeScript
- Map: MapLibre GL 5.x (event-driven, callback patterns)
- DB: Prisma 7 + Neon (repository pattern already applied)
- State: React hooks (no Redux/Zustand — client-side state in map page component)
- Geocoding: 3-tier cascade (Jawg → Geoapify → Nominatim) — Strategy pattern candidate
Operating Modes
Mode 1: Detection
Trigger: User requests pattern detection or analysis
Output: Report of patterns found with confidence scores and stack context
Workflow:
1. Stack Detection (package.json, tsconfig.json, framework files)
2. Pattern Search (Glob for candidates → Grep for signatures → Read for validation)
3. Classification (native to stack vs custom implementations)
4. Confidence Scoring (0.0-1.0 based on detection rules)
5. Report Generation
Mode 2: Suggestion
Trigger: User requests pattern suggestions or refactoring advice
Output: Markdown report with prioritized suggestions and stack-adapted examples
Workflow:
1. Code Smell Detection (switch statements, long parameter lists, global state, etc.)
2. Pattern Matching (map smell → applicable patterns)
3. Stack Adaptation (prefer native framework patterns over custom implementations)
4. Priority Ranking (impact × feasibility)
5. Markdown Report with Code Examples
Mode 3: Evaluation
Trigger: User requests pattern quality assessment
Output: Report with scores per evaluation criterion
Methodology
Phase 1: Stack Detection
Sources (in priority order):
package.json → Check dependencies and devDependencies
tsconfig.json → Check compilerOptions, paths
- File extensions →
*.tsx presence count
Phase 2: Pattern Detection
Search Strategy:
Glob Phase: Find candidate files by naming convention
*Singleton*.ts, *Factory*.ts, *Strategy*.ts, *Observer*.ts, etc.
Grep Phase: Search for pattern signatures
- Primary signals:
private constructor, static getInstance(), subscribe(), createXxx(), etc.
Read Phase: Validate pattern structure
Phase 3: Code Smell Detection
Target Smells:
- Switch on Type → Strategy/Factory pattern
- Long Parameter List (>4) → Builder pattern
- Global State Access → Singleton (or preferably React Context)
- Duplicated Conditionals on State → State pattern
- Scattered Notification Logic → Observer pattern
- Complex Object Creation → Factory/Abstract Factory
- Tight Coupling to Concrete Classes → Adapter/Bridge
- Large Class with Many Responsibilities → Facade pattern
Phase 4: Stack-Aware Suggestions
StarMapper-specific adaptations:
| Pattern |
Current usage |
Recommendation |
| Strategy |
Geocoding cascade (if/else chain) |
Extract to strategy objects: JawgStrategy, GeoapifyStrategy, NominatimStrategy |
| Observer |
React useState in map page |
Already idiomatic — use React state |
| Singleton |
prisma in db.ts |
Already correct — PrismaClient singleton |
| Facade |
fetchAndPatchStyle() in map-style.ts |
Already applied — keep as is |
| Iterator |
Chunk loop in page.tsx |
Already idiomatic async iterator pattern |
Phase 5: Quality Evaluation
Criteria:
- Correctness (0-10): Does it match the canonical pattern structure?
- Testability (0-10): Can dependencies be mocked/stubbed easily?
- Single Responsibility (0-10): Does it do one thing only?
- Open/Closed Principle (0-10): Extensible without modification?
- Documentation (0-10): Clear intent, descriptive naming?
Output Format
Detection Mode
## Design Patterns Found
### Singleton — src/lib/db.ts:1-15
- Confidence: 0.95
- Type: custom
- Signals: PrismaClient singleton, module-level cache
- Note: Correct pattern for Next.js serverless
### Facade — src/lib/map-style.ts:1-30
- Confidence: 0.88
- Type: custom
- Signals: fetchAndPatchStyle() encapsulates tile URL patching
### Strategy (candidate) — src/lib/geocoder.ts:45-120
- Confidence: 0.60
- Type: implicit (if/else chain, not extracted)
- Suggestion: Extract Jawg/Geoapify/Nominatim as strategy objects for testability
Suggestion Mode
## Design Pattern Suggestions
### 1. Strategy Pattern → src/lib/geocoder.ts
**Code Smell**: 3-tier if/else cascade — hard to test individual providers
**Current**: if (jawgResult) ... else if (geoapifyResult) ... else nominatimResult
**Recommended**: Extract strategy objects with a common interface
interface GeocodingStrategy {
geocode(location: string): Promise<LatLng | null>;
}
const jawgStrategy: GeocodingStrategy = { ... };
const geoapifyStrategy: GeocodingStrategy = { ... };
const nominatimStrategy: GeocodingStrategy = { ... };
const strategies = [jawgStrategy, geoapifyStrategy, nominatimStrategy];
// Waterfall through strategies
Constraints & Guidelines
Read-Only Analysis
- No modifications: This skill only analyzes and suggests, never modifies code
- User decision: All suggestions require explicit user approval before implementation
Language Focus
- Primary: TypeScript (
.ts, .tsx)
- Exclusions: Other languages not supported
Pattern Coverage
- Creational (5): Singleton, Factory Method, Abstract Factory, Builder, Prototype
- Structural (7): Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
- Behavioral (11): Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor, Interpreter
Usage Examples
# Detect all patterns in src/
/design-patterns detect src/
# Get suggestions for geocoder
/design-patterns suggest src/lib/geocoder.ts
# Evaluate singleton in db.ts
/design-patterns evaluate src/lib/db.ts
Skill Version: 1.0.0
Pattern Coverage: 23 GoF patterns
Supported Stacks: React, Next.js, Prisma, MapLibre
1---2name: design-patterns-23description: Use when analyzing a codebase for GoF design patterns, or when refactoring and needing pattern suggestions. Stack-aware (TypeScript/React/Next.js adaptations).4---56# Design Patterns Analyzer Skill78**Purpose**: Detect, suggest, and evaluate Gang of Four (GoF) design patterns in TypeScript/JavaScript codebases with stack-aware adaptations.910## Core Capabilities11121. **Stack Detection**: Identify primary framework/library (React, Next.js, MapLibre, Prisma)132. **Pattern Detection**: Find existing implementations of 23 GoF patterns143. **Smart Suggestions**: Recommend patterns to fix code smells, using stack-native idioms when available154. **Quality Evaluation**: Assess pattern implementation quality against best practices1617## StarMapper Stack Context1819- **Primary**: React 18 + Next.js App Router + TypeScript20- **Map**: MapLibre GL 5.x (event-driven, callback patterns)21- **DB**: Prisma 7 + Neon (repository pattern already applied)22- **State**: React hooks (no Redux/Zustand — client-side state in map page component)23- **Geocoding**: 3-tier cascade (Jawg → Geoapify → Nominatim) — Strategy pattern candidate2425## Operating Modes2627### Mode 1: Detection2829**Trigger**: User requests pattern detection or analysis30**Output**: Report of patterns found with confidence scores and stack context3132**Workflow**:3334```351. Stack Detection (package.json, tsconfig.json, framework files)362. Pattern Search (Glob for candidates → Grep for signatures → Read for validation)373. Classification (native to stack vs custom implementations)384. Confidence Scoring (0.0-1.0 based on detection rules)395. Report Generation40```4142### Mode 2: Suggestion4344**Trigger**: User requests pattern suggestions or refactoring advice45**Output**: Markdown report with prioritized suggestions and stack-adapted examples4647**Workflow**:4849```501. Code Smell Detection (switch statements, long parameter lists, global state, etc.)512. Pattern Matching (map smell → applicable patterns)523. Stack Adaptation (prefer native framework patterns over custom implementations)534. Priority Ranking (impact × feasibility)545. Markdown Report with Code Examples55```5657### Mode 3: Evaluation5859**Trigger**: User requests pattern quality assessment60**Output**: Report with scores per evaluation criterion6162## Methodology6364### Phase 1: Stack Detection6566**Sources** (in priority order):67681. `package.json` → Check dependencies and devDependencies692. `tsconfig.json` → Check compilerOptions, paths703. File extensions → `*.tsx` presence count7172### Phase 2: Pattern Detection7374**Search Strategy**:75761. **Glob Phase**: Find candidate files by naming convention77 - `*Singleton*.ts`, `*Factory*.ts`, `*Strategy*.ts`, `*Observer*.ts`, etc.78792. **Grep Phase**: Search for pattern signatures80 - Primary signals: `private constructor`, `static getInstance()`, `subscribe()`, `createXxx()`, etc.81823. **Read Phase**: Validate pattern structure8384### Phase 3: Code Smell Detection8586**Target Smells**:87881. **Switch on Type** → Strategy/Factory pattern892. **Long Parameter List (>4)** → Builder pattern903. **Global State Access** → Singleton (or preferably React Context)914. **Duplicated Conditionals on State** → State pattern925. **Scattered Notification Logic** → Observer pattern936. **Complex Object Creation** → Factory/Abstract Factory947. **Tight Coupling to Concrete Classes** → Adapter/Bridge958. **Large Class with Many Responsibilities** → Facade pattern9697### Phase 4: Stack-Aware Suggestions9899**StarMapper-specific adaptations**:100101| Pattern | Current usage | Recommendation |102|---------|--------------|----------------|103| Strategy | Geocoding cascade (if/else chain) | Extract to strategy objects: `JawgStrategy`, `GeoapifyStrategy`, `NominatimStrategy` |104| Observer | React useState in map page | Already idiomatic — use React state |105| Singleton | `prisma` in `db.ts` | Already correct — PrismaClient singleton |106| Facade | `fetchAndPatchStyle()` in `map-style.ts` | Already applied — keep as is |107| Iterator | Chunk loop in page.tsx | Already idiomatic async iterator pattern |108109### Phase 5: Quality Evaluation110111**Criteria**:1121131. **Correctness (0-10)**: Does it match the canonical pattern structure?1142. **Testability (0-10)**: Can dependencies be mocked/stubbed easily?1153. **Single Responsibility (0-10)**: Does it do one thing only?1164. **Open/Closed Principle (0-10)**: Extensible without modification?1175. **Documentation (0-10)**: Clear intent, descriptive naming?118119## Output Format120121### Detection Mode122123```124## Design Patterns Found125126### Singleton — src/lib/db.ts:1-15127- Confidence: 0.95128- Type: custom129- Signals: PrismaClient singleton, module-level cache130- Note: Correct pattern for Next.js serverless131132### Facade — src/lib/map-style.ts:1-30133- Confidence: 0.88134- Type: custom135- Signals: fetchAndPatchStyle() encapsulates tile URL patching136137### Strategy (candidate) — src/lib/geocoder.ts:45-120138- Confidence: 0.60139- Type: implicit (if/else chain, not extracted)140- Suggestion: Extract Jawg/Geoapify/Nominatim as strategy objects for testability141```142143### Suggestion Mode144145```markdown146## Design Pattern Suggestions147148### 1. Strategy Pattern → src/lib/geocoder.ts149150**Code Smell**: 3-tier if/else cascade — hard to test individual providers151152**Current**: if (jawgResult) ... else if (geoapifyResult) ... else nominatimResult153**Recommended**: Extract strategy objects with a common interface154155interface GeocodingStrategy {156 geocode(location: string): Promise<LatLng | null>;157}158159const jawgStrategy: GeocodingStrategy = { ... };160const geoapifyStrategy: GeocodingStrategy = { ... };161const nominatimStrategy: GeocodingStrategy = { ... };162163const strategies = [jawgStrategy, geoapifyStrategy, nominatimStrategy];164// Waterfall through strategies165```166167## Constraints & Guidelines168169### Read-Only Analysis170171- **No modifications**: This skill only analyzes and suggests, never modifies code172- **User decision**: All suggestions require explicit user approval before implementation173174### Language Focus175176- **Primary**: TypeScript (`.ts`, `.tsx`)177- **Exclusions**: Other languages not supported178179### Pattern Coverage180181- **Creational (5)**: Singleton, Factory Method, Abstract Factory, Builder, Prototype182- **Structural (7)**: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy183- **Behavioral (11)**: Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor, Interpreter184185## Usage Examples186187```bash188# Detect all patterns in src/189/design-patterns detect src/190191# Get suggestions for geocoder192/design-patterns suggest src/lib/geocoder.ts193194# Evaluate singleton in db.ts195/design-patterns evaluate src/lib/db.ts196```197198**Skill Version**: 1.0.0199**Pattern Coverage**: 23 GoF patterns200**Supported Stacks**: React, Next.js, Prisma, MapLibre