목표
현재 상태
계산 함수에 직접 들어가 조건문을 수정
// 연극의 종류가 계산 방식을 선택하는데 중심적인 역할을 함
// 수정이 이루어질수록 복잡해지고 손상될 것임
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.play.type) {
case "tragedy":
result = 40000;
if (aPerformance.audience > 30) {
result += 1000 * (aPerformance.audience - 30);
}
break;
case "comedy":
result = 30000;
if (aPerformance.audience > 20) {
result += 10000 + 500 * (aPerformance.audience - 20);
}
result += 300 * aPerformance.audience;
break;
default:
throw new Error(`unknown type: ${aPerformance.play.type}`);
}
return result;
}
해야할 것
방법
핵심 리팩토링 기법
*Replace Conditional with Polymorphism (272) 조건문을 다형성으로 대체*
클래스 생성: amount와 volumeCredits 로직을 담을 클래스를 정의.
기존 계산 코드 점검
Creating a Performance Calculator
Moving Functions into the Calculator
Making the Performance Calculator Polymorphic