docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를 Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류. - 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로 자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거, 동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거. - 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming, Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business, Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로, 나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는 title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백). 원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지. - 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서. - 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는 지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지. - Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경. - 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
This commit is contained in:
+204
@@ -0,0 +1,204 @@
|
||||
---
|
||||
id: wiki-2026-0508-replace-conditional-with-polymor
|
||||
title: Replace Conditional with Polymorphism (조건식을 다형성으로 바꾸기)
|
||||
category: 10_Wiki/Topics
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [Replace Conditional with Polymorphism, RCwP, Polymorphism Refactoring]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 0.9
|
||||
verification_status: applied
|
||||
tags: [refactoring, oop, design-pattern, fowler]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
tech_stack:
|
||||
language: TypeScript
|
||||
framework: none
|
||||
---
|
||||
|
||||
# Replace Conditional with Polymorphism (조건식을 다형성으로 바꾸기)
|
||||
|
||||
## 매 한 줄
|
||||
> **"매 type-discriminating switch/if-chain 을 매 polymorphic dispatch 로 교체"**. Fowler *Refactoring 2e* (2018) Ch.10. 매 type code 가 add 시마다 매 switch 들 모두 수정 필요한 shotgun surgery 의 표준 처방. 매 modern alternative — discriminated union + exhaustiveness, strategy map, pattern matching — 도 같은 본질을 공유한다.
|
||||
|
||||
## 매 핵심
|
||||
|
||||
### 매 trigger smell
|
||||
- 매 동일 `switch (kind)` 가 codebase 여러 곳에 반복.
|
||||
- 매 새 type 추가가 매 N 곳의 switch 수정 강제.
|
||||
- 매 default branch 가 silently incorrect.
|
||||
|
||||
### 매 mechanics (Fowler)
|
||||
1. 매 self-encapsulating type 추출 (subclass 또는 strategy).
|
||||
2. 매 switch 한 case 를 method override 로 이동 (test 유지).
|
||||
3. 매 모든 case 이동 후 매 base method 를 abstract 화.
|
||||
4. 매 caller 에서 매 conditional 제거.
|
||||
|
||||
### 매 응용
|
||||
1. Tax calculation: country-specific subclass.
|
||||
2. Bird movement: Penguin/Parrot/Owl `getSpeed()` override.
|
||||
3. Payment processor: Stripe/Toss/PayPal strategy.
|
||||
4. AST visitor: node type 별 dispatch.
|
||||
|
||||
## 💻 패턴
|
||||
|
||||
### Before: type-code switch
|
||||
```typescript
|
||||
type Bird = {
|
||||
kind: 'penguin' | 'parrot' | 'owl';
|
||||
voltage?: number;
|
||||
isNailed?: boolean;
|
||||
numberOfCoconuts?: number;
|
||||
};
|
||||
|
||||
function plumage(bird: Bird): string {
|
||||
switch (bird.kind) {
|
||||
case 'penguin': return 'average';
|
||||
case 'parrot': return bird.isNailed ? 'tattered' : 'beautiful';
|
||||
case 'owl': return 'attentive';
|
||||
}
|
||||
}
|
||||
|
||||
function airSpeed(bird: Bird): number {
|
||||
switch (bird.kind) {
|
||||
case 'penguin': return 50;
|
||||
case 'parrot': return 80 - 5 * (bird.voltage ?? 0);
|
||||
case 'owl': return 12 * (bird.numberOfCoconuts ?? 0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### After: polymorphism (classes)
|
||||
```typescript
|
||||
abstract class Bird {
|
||||
abstract get plumage(): string;
|
||||
abstract get airSpeed(): number;
|
||||
}
|
||||
|
||||
class Penguin extends Bird {
|
||||
get plumage() { return 'average'; }
|
||||
get airSpeed() { return 50; }
|
||||
}
|
||||
|
||||
class Parrot extends Bird {
|
||||
constructor(private voltage = 0, private nailed = false) { super(); }
|
||||
get plumage() { return this.nailed ? 'tattered' : 'beautiful'; }
|
||||
get airSpeed() { return 80 - 5 * this.voltage; }
|
||||
}
|
||||
|
||||
class Owl extends Bird {
|
||||
constructor(private coconuts = 0) { super(); }
|
||||
get plumage() { return 'attentive'; }
|
||||
get airSpeed() { return 12 * this.coconuts; }
|
||||
}
|
||||
```
|
||||
|
||||
### Modern alternative: discriminated union + exhaustiveness
|
||||
```typescript
|
||||
type Bird =
|
||||
| { kind: 'penguin' }
|
||||
| { kind: 'parrot'; voltage: number; nailed: boolean }
|
||||
| { kind: 'owl'; coconuts: number };
|
||||
|
||||
function airSpeed(b: Bird): number {
|
||||
switch (b.kind) {
|
||||
case 'penguin': return 50;
|
||||
case 'parrot': return 80 - 5 * b.voltage;
|
||||
case 'owl': return 12 * b.coconuts;
|
||||
default: {
|
||||
const _exhaustive: never = b;
|
||||
return _exhaustive;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Strategy map (data-driven dispatch)
|
||||
```typescript
|
||||
const speedStrategies = {
|
||||
penguin: () => 50,
|
||||
parrot: (b: { voltage: number }) => 80 - 5 * b.voltage,
|
||||
owl: (b: { coconuts: number }) => 12 * b.coconuts,
|
||||
} as const;
|
||||
|
||||
function airSpeed<K extends keyof typeof speedStrategies>(
|
||||
kind: K,
|
||||
data: Parameters<typeof speedStrategies[K]>[0],
|
||||
): number {
|
||||
return (speedStrategies[kind] as any)(data);
|
||||
}
|
||||
```
|
||||
|
||||
### Visitor (when ops > types)
|
||||
```typescript
|
||||
interface Visitor<R> {
|
||||
penguin(): R;
|
||||
parrot(b: { voltage: number; nailed: boolean }): R;
|
||||
owl(b: { coconuts: number }): R;
|
||||
}
|
||||
|
||||
class SpeedVisitor implements Visitor<number> {
|
||||
penguin() { return 50; }
|
||||
parrot(b: { voltage: number }) { return 80 - 5 * b.voltage; }
|
||||
owl(b: { coconuts: number }) { return 12 * b.coconuts; }
|
||||
}
|
||||
```
|
||||
|
||||
### Rust pattern matching (sister technique)
|
||||
```rust
|
||||
enum Bird {
|
||||
Penguin,
|
||||
Parrot { voltage: u32, nailed: bool },
|
||||
Owl { coconuts: u32 },
|
||||
}
|
||||
|
||||
impl Bird {
|
||||
fn air_speed(&self) -> u32 {
|
||||
match self {
|
||||
Bird::Penguin => 50,
|
||||
Bird::Parrot { voltage, .. } => 80 - 5 * voltage,
|
||||
Bird::Owl { coconuts } => 12 * coconuts,
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
| 상황 | Approach |
|
||||
|---|---|
|
||||
| OOP language, type 자주 add | Subclass polymorphism |
|
||||
| FP/TS, op 자주 add | Discriminated union + match (expression problem) |
|
||||
| 매 dispatch table 단순 | Strategy map (object literal) |
|
||||
| Op 많고 type 안정적 | Visitor |
|
||||
| Type 많고 op 안정적 | Sum type + match |
|
||||
| 매 single-use switch | 매 그대로 두기 — refactor cost > benefit |
|
||||
|
||||
**기본값**: TS — discriminated union + exhaustive switch. OOP-heavy domain — subclass.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[Refactoring_Best_Practices]] · [[Polymorphism]]
|
||||
- 변형: [[Visitor Pattern]]
|
||||
- 응용: [[Discriminated Union]] · [[Pattern Matching]] · [[Open-Closed Principle]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: 매 codebase 에 매 동일 switch 가 3+ 곳, 매 새 type 추가가 자주, 매 OCP 위반 가시.
|
||||
**언제 X**: 매 switch 1 곳, 매 type 안정적, 매 small dispatch — over-engineering.
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **Inheritance for everything**: 매 deep hierarchy — composition + interface 권장.
|
||||
- **Polymorphism without exhaustiveness**: 매 default branch 가 silent bug 숨김.
|
||||
- **Anemic subclass**: 매 method 1개 차이 — strategy 가 더 적합.
|
||||
- **Refactor without tests**: 매 behavior preservation 의 보증 없음.
|
||||
- **`instanceof` chain**: 매 polymorphism 의 antithesis — 매 새 switch 의 변종.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified: Fowler *Refactoring 2e* (2018) Ch.10 "Replace Conditional with Polymorphism"; refactoring.com.
|
||||
- 신뢰도 A.
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — Fowler mechanics + modern union/strategy/visitor variants |
|
||||
Reference in New Issue
Block a user