f8b21af4be
10_Wiki/Topics 대규모 정리: - 오류 캡처/미완성 stub 문서 227개 제거 - 교차폴더 중복 43클러스터 병합 (63파일 → redirect) - 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건 - 카테고리 MOC 6개 신규 생성 - Graph 섹션 미해결 related-keyword 링크 10,058건 제거 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
207 lines
6.4 KiB
Markdown
207 lines
6.4 KiB
Markdown
---
|
|
id: wiki-2026-0508-extract-class-클래스-추출하기
|
|
title: Extract Class (클래스 추출하기)
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [Extract Class, 클래스 추출, Refactoring]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.9
|
|
verification_status: applied
|
|
tags: [refactoring, fowler, srp, oop]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: typescript
|
|
framework: none
|
|
---
|
|
|
|
# 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)
|
|
1. 매 responsibility 의 identify — 매 split point.
|
|
2. 매 new child class 의 create.
|
|
3. 매 parent → child reference 의 establish.
|
|
4. 매 fields 의 move (Move Field).
|
|
5. 매 methods 의 move (Move Method) — 매 leaves first.
|
|
6. 매 child interface 의 review — 매 internal exposure 의 minimize.
|
|
7. 매 expose: 매 child 의 public 의 또는 parent-only 의 decision.
|
|
|
|
### 매 응용
|
|
1. Person → Person + TelephoneNumber.
|
|
2. Order → Order + ShippingAddress + BillingAddress.
|
|
3. UserService → UserService + AuthService + ProfileService.
|
|
4. GameEntity → Position + Velocity + Health (ECS).
|
|
|
|
## 💻 패턴
|
|
|
|
### Before — God class
|
|
```typescript
|
|
class Person {
|
|
name: string;
|
|
officeAreaCode: string;
|
|
officeNumber: string;
|
|
|
|
getTelephoneNumber(): string {
|
|
return `(${this.officeAreaCode}) ${this.officeNumber}`;
|
|
}
|
|
}
|
|
```
|
|
|
|
### After — Extract Class
|
|
```typescript
|
|
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
|
|
```typescript
|
|
// 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)
|
|
```typescript
|
|
// 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
|
|
```typescript
|
|
// 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
|
|
```typescript
|
|
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_Best_Practices|Refactoring]] · [[SOLID]]
|
|
- 응용: [[Single Responsibility Principle (SRP)|Single-Responsibility-Principle]] · [[Entity-Component-System]]
|
|
|
|
## 🤖 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 |
|