AgentSkillsCN

oop-specialist

在编写或审查 TypeScript + Next.js 代码库时,灵活运用面向对象设计原则(SOLID/GRASP)。无论是设计或重构服务、类库、领域模型,还是划定模块边界,此方法都大有裨益;尤其当代码出现高耦合、低内聚、业务规则重复,或职责划分模糊时,更需及时加以干预。

SKILL.md
--- frontmatter
name: oop-specialist
description: Apply object-oriented design principles (SOLID/GRASP) when writing or reviewing code in this TypeScript + Next.js codebase. Use when designing/refactoring services, libraries, domain models, and module boundaries; triggers on high coupling, low cohesion, duplicated business rules, and unclear responsibilities.

OOP Specialist

Apply these principles when designing, writing, or reviewing object-oriented code.

Core Constraints (Heuristics)

  • Keep methods small and intention-revealing (extract aggressively)
  • Keep classes/modules focused (one reason to change)
  • Prefer parameter objects over long parameter lists

SOLID Principles

PrincipleRuleViolation Sign
SRPOne reason to changeClass name includes "And" or "Manager"
OCPExtend, don't modifyAdding features requires changing existing code
LSPSubtypes substitute baseOverridden method raises unexpected errors
ISPSmall, specific interfacesImplementing empty/stub methods
DIPDepend on abstractionsInstantiating dependencies directly

GRASP Principles

  • Information Expert: Assign responsibility to class with the data
  • Creator: Create objects where they're contained/aggregated/used
  • Low Coupling: Minimize dependencies between classes
  • High Cohesion: Keep class responsibilities focused
  • Controller: Delegate UI events to controller, not domain objects
  • Polymorphism: Replace conditionals on type with polymorphic methods
  • Pure Fabrication: Create utility classes when no domain class fits
  • Protected Variations: Hide change-prone areas behind interfaces

Key Heuristics

Tell, Don't Ask

ts
// Bad: pull data out, decide elsewhere
if (order.status === "pending" && order.totalCents > 10_000) {
  order.applyDiscountPercent(10);
}

// Good: tell the object what to do
order.applyDiscountIfEligible();

Composition Over Inheritance

Use inheritance only when child "is-a" parent. Prefer composition for "has-a" or "uses-a" relationships.

ts
type Item = { id: string; priceCents: number };
type PricingStrategy = { priceTotalCents: (items: Item[]) => number };

class Order {
  constructor(private readonly pricingStrategy: PricingStrategy) {}

  totalCents(items: Item[]) {
    return this.pricingStrategy.priceTotalCents(items);
  }
}

DRY (Don't Repeat Yourself)

Extract duplicated logic. Three occurrences = extract immediately.

Design Patterns Quick Reference

PatternUse When
FactoryObject creation logic is complex or varies
BuilderConstructing complex objects step-by-step
StrategyAlgorithms vary and should be interchangeable
StateBehavior changes based on internal state
FacadeSimplifying a complex subsystem interface
Template MethodAlgorithm structure fixed, steps vary
Null ObjectAvoiding nil checks with default behavior

Functional Patterns

Prefer map, filter/select, reduce over manual iteration:

ts
// Bad
const results: string[] = [];
for (const item of items) {
  if (item.active) results.push(item.name);
}

// Good
const results = items.filter((i) => i.active).map((i) => i.name);

Clean Architecture Layers

code
┌─────────────────────────────────────┐
│  Frameworks & Drivers (outer)       │  ← Next.js, React, Prisma/Postgres
├─────────────────────────────────────┤
│  Interface Adapters                 │  ← Controllers, Serializers
├─────────────────────────────────────┤
│  Use Cases                          │  ← Service Objects
├─────────────────────────────────────┤
│  Entities (inner)                   │  ← Domain Models
└─────────────────────────────────────┘

Dependencies point INWARD only.

In this repo, a practical mapping is:

  • Frameworks & drivers: Next.js App Router, NextAuth, Prisma, Postgres
  • Interface adapters: src/app/* pages, route handlers, server actions
  • Use cases: src/services/* (business rules)
  • Entities: Prisma models + domain types

References

For detailed patterns and examples:

oop-specialist - AgentSkills CN | 技能图谱