function base(aReading) {...}
function taxableCharge(aReading) {...}
function calculateBaseCharge(aReading) {...}
class Reading {
base() {...}
taxableCharge() {...}
calculateBaseCharge() {...}
}
공통 데이터를 중심으로 작동하는 함수 무리를 발견하면 클래스 하나로 묶는다. 이미 만들어진 함수를 재구성할 때는 물론, 새로 만든 클래스와 관련하여 놓친 연산을 찾아서 새 클래스의 메서드로 뽑아내는데도 좋다. 저자는 중첩 함수보다 클래스를 선호한다.
reading = {customer: "ivan", quantity: 10, month: 5, year: 2017}
// 클라이언트1
const aReading = acquireReading();
const baseCharge = baseRate(areadong.month, aReading.year) * aReading.quantity;
// 클라이언트2
const aReading = acquireReading();
const base = baseRate(areadong.month, aReading.year) * aReading.quantity;
const taxableCharge = Mtah.max(0, base - taxThreshole(aReading.year));
// 클라이언트3
const aReading = acquireReading();
const basicChargeAmount = calculateBaseCharge(aReading);
const calculateBaseCharge(aReading) {
return baseRate(areadong.month, aReading.year) * aReading.quantity;
}
class Reading {
constructor(data) {
this._customer = data.customer;
this._quantity = data.quantity;
this._month = data.month;
this._year = data.year;
} // 1. 레코드를 클래스로 변환하기 위해 레코드를 캡슐화한다.
get customer() { return this._custormer };
get quantity() { return this._quantity };
get month() { return this._month };
get year() { return this._year };
get baseCharge(aReading) {
return baseRate(this.month, this.year) * this.quantity;
}
}
get taxableCharge(aReading) {
return Mtah.max(0, aReading.baseCharge - taxThreshole(aReading.year));
}
// 클라이언트1
const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const baseCharge = aReading.baseCharge; // 중복된 계산 코드를 고쳐 앞의 메서드를 호출하게 한다.
// 클라이언트2
const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const taxableCharge = Mtah.max(0, aReading.baseCharge - taxThreshole(aReading.year));
// 클라이언트3
const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const taxableCharge = aReading.taxableCharge;