6.5 KiB
6.5 KiB
id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
| id | title | category | status | canonical_id | aliases | duplicate_of | source_trust_level | confidence_score | verification_status | tags | raw_sources | last_reinforced | github_commit | tech_stack | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| wiki-2026-0508-extract-class-클래스-추출하기 | Extract Class (클래스 추출하기) | 10_Wiki/Topics | verified | self |
|
none | A | 0.9 | applied |
|
2026-05-10 | pending |
|
Extract Class (클래스 추출하기)
매 한 줄
"매 too-much-doing class 의 cohesive subset 의 split". Extract Class 매 Fowler refactoring catalog 의 core move — 매 single class 매 multiple responsibilities 의 carry, 매 data + methods 의 cohesive cluster 의 new class 의 extract. SRP 의 enforcement.
매 핵심
매 Smell signals
- 매 class 매 200+ lines 의 grow.
- 매 fields 의 subset 매 always 의 used together (e.g.,
street/city/zip의 always together). - 매 methods 매 disjoint groups 의 form — 매 group A methods 매 group A fields only 의 use.
- 매 class name 매 vague —
Manager,Helper,Util.
매 Mechanics (Fowler)
- 매 responsibility 의 identify — 매 split point.
- 매 new child class 의 create.
- 매 parent → child reference 의 establish.
- 매 fields 의 move (Move Field).
- 매 methods 의 move (Move Method) — 매 leaves first.
- 매 child interface 의 review — 매 internal exposure 의 minimize.
- 매 expose: 매 child 의 public 의 또는 parent-only 의 decision.
매 응용
- Person → Person + TelephoneNumber.
- Order → Order + ShippingAddress + BillingAddress.
- UserService → UserService + AuthService + ProfileService.
- GameEntity → Position + Velocity + Health (ECS).
💻 패턴
Before — God class
class Person {
name: string;
officeAreaCode: string;
officeNumber: string;
getTelephoneNumber(): string {
return `(${this.officeAreaCode}) ${this.officeNumber}`;
}
}
After — Extract Class
class TelephoneNumber {
constructor(public areaCode: string, public number: string) {}
toString(): string { return `(${this.areaCode}) ${this.number}`; }
}
class Person {
constructor(
public name: string,
private officeTelephone: TelephoneNumber,
) {}
get telephoneNumber(): string { return this.officeTelephone.toString(); }
// delegate accessors during transition
get officeAreaCode() { return this.officeTelephone.areaCode; }
set officeAreaCode(v: string) { this.officeTelephone.areaCode = v; }
}
Address extraction
// Before
class Order {
customerName: string;
street: string; city: string; zipCode: string; country: string;
shippingCost: number;
}
// After
class Address {
constructor(
public street: string,
public city: string,
public zipCode: string,
public country: string,
) {}
format(): string { return `${this.street}, ${this.city} ${this.zipCode}, ${this.country}`; }
isInternational(home: string) { return this.country !== home; }
}
class Order {
constructor(
public customerName: string,
public shippingAddress: Address,
public shippingCost: number,
) {}
}
Service split (SRP)
// Before — 600 LOC
class UserService {
signup() { /* ... */ }
login() { /* ... */ }
resetPassword() { /* ... */ }
updateProfile() { /* ... */ }
uploadAvatar() { /* ... */ }
getPreferences() { /* ... */ }
}
// After
class AuthService {
signup() {} login() {} resetPassword() {}
}
class ProfileService {
updateProfile() {} uploadAvatar() {} getPreferences() {}
}
// UserService 의 orchestration only 의 keep
ECS-style field group extraction
// Before — game entity 의 carries everything
class Enemy {
x: number; y: number; vx: number; vy: number;
hp: number; maxHp: number; armor: number;
spriteId: string; animFrame: number;
}
// After — components
class Position { constructor(public x: number, public y: number) {} }
class Velocity { constructor(public vx: number, public vy: number) {} }
class Health { constructor(public hp: number, public maxHp: number, public armor = 0) {} }
class Sprite { constructor(public id: string, public frame = 0) {} }
class Enemy {
constructor(
public position: Position,
public velocity: Velocity,
public health: Health,
public sprite: Sprite,
) {}
}
Incremental migration via delegation
class Person {
private _phone = new TelephoneNumber('', '');
// Old API 의 keep working
get officeNumber() { return this._phone.number; }
set officeNumber(v: string) { this._phone.number = v; }
// New API 의 prefer
get phone() { return this._phone; }
}
// Phase 1: delegate. Phase 2: callers migrate to .phone. Phase 3: remove old getters.
매 결정 기준
| 상황 | Approach |
|---|---|
| Class < 100 LOC, single concept | 매 extract 매 X — 매 premature |
| Field subset always 의 together | 매 Extract Class |
| Two distinct responsibilities | 매 Extract Class + Move Method |
| Component reuse 의 needed (game) | 매 ECS-style components 의 extract |
| Public API 의 break 의 dangerous | 매 delegating accessors 의 transition |
기본값: 매 class 매 2 의 distinct responsibility 의 carry 의 extract — 매 cohesion > 1 class.
🔗 Graph
- 부모: Refactoring · SOLID
- 변형: Extract-Method · Extract-Interface · Move-Field
- 응용: Single-Responsibility-Principle · Entity-Component-System
- Adjacent: God-Class-Antipattern · Inline-Class
🤖 LLM 활용
언제: 매 class 매 200+ LOC, 매 multiple distinct concerns. Field cluster 의 always 의 co-occur. Test setup 매 unrelated mocks 의 require. 언제 X: 매 small DTO. 매 abstraction 의 cost > readability gain (매 over-extraction).
❌ 안티패턴
- Anemic extraction: 매 fields only 의 move, 매 methods 매 parent 의 stay — 매 just data bag 의 result. Methods 의 follow 의 must.
- Reverse coupling: 매 child 매 parent 의 reference back — 매 cyclic dependency. One-way reference 의 keep.
- Premature extract: 매 50 LOC class 의 split — 매 navigation overhead > clarity.
- Public sprawl: 매 child 의 immediately public — 매 hide 매 first, 매 expose 의 demand 의 occur 시.
🧪 검증 / 중복
- Verified (Fowler "Refactoring" 2nd ed., Ch. 7).
- 신뢰도 A.
🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — Extract Class refactoring 의 mechanics + 5 patterns |