✏️10.4 조건부 로직을 다형성으로 바꾸기 (Replace Conditional with Polymorphism)
switch (bird.type) {
case '유럽 제비':
return "보통이다";
case '아프리카 제비':
return (bird.numberOfCoconuts > 2) ? "지쳤다" : "보통이다";
case 'NorwegianBlueParrot':
return (bird.voltage > 100) ? "그을렸다" : "예쁘다";
default:
return "알 수 없다";class EuropeanSwallow {
get plumage() {
return "보통이다";
}
...
class AfricanSwallow {
get plumage() {
return (this.numberOfCoconuts > 2) ? "지쳤다" : "보통이다";
}
...
class NorwegianBlueParrot {
get plumage() {
return (this.voltage > 100) ? "그을렸다" : "예쁘다";
}🧷 배경
🧷 절차
🧷 예시
🧷 리팩터링 전
🧷 리팩터링 후
Previous10.3 중첩 조건문을 보호 구문으로 바꾸기 (Replace Nested Conditional with Guard Clauses)Next10.5 특이 케이스 추가하기 (Introduce Special Case)
Last updated