๋ง์ง๋ง์ผ๋ก ๋ณ๊ฒฝ๋ ์ฝ๋๋ฅผ ๋ณด๋ฉฐ ์๊ฐํด๋ณด์.
์ํ ์ ๊ฒ: ๋คํ์ฑ์ ํ์ฉํ์ฌ ๋ฐ์ดํฐ ์์ฑํ๊ธฐ
// createStatementData.js
function createPerformanceCalculator(aPerformance, aPlay) {
switch(aPlay.type) {
case "tragedy":
return new TragedyCalculator(aPerformance, aPlay);
case "comedy":
return new ComedyCalculator(aPerformance, aPlay);
default:
throw new Error('์ ์ ์๋ ์ฅ๋ฅด: ${aPlay.type}');
}
}
class TragedyCalculator(aPerformance, aPlay) {
get amount() {
let result = 40000;
if (this.performance.audience > 30) {
result += 1000 * (this.performance.audience - 30);
}
return result;
}
}
class ComedyCalculator(aPerformance, aPlay) {
get amount() {
let result = 30000;
if (this.performance.audience > 20) {
result += 10000 + 500 * (this.performance.audience - 20);
}
result += 300 * this.performance.audience;
return result;
}
get volumeCredits() { // ๋ณ๊ฒฝ๋จ
super.volumeCredits + Math.floor(this.aPerformance.audience / 5);
}
}
class PerformanceCalculator {
constructor(aPerformance, aPlay) {
this.performance = aPerformance;
this.play = aPlay;
}
get amount() {
throw new Error('์๋ธํด๋์ค์์ ์ฒ๋ฆฌํ๋๋ก ์ค๊ณ๋์์ต๋๋ค.');
}
get volumeCredits() {
Math.max(this.aPerformance.audience - 30, 0);
}
}
export default function createStatementData(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
statementData.totalAmount = totalAmount();
statementData.totalVolumeCredits = totalVolumeCredits();
return statementData;
function enrichPerformance(aPerformance) {
const calculator = createPerformanceCalculator(aPerformance, playFor(aPerformance));
const result = Object.assign({}, aPerformance);
result.play = calculator.play;
result.amount = calculator.amount();
result.volumeCredits = calculator.volumeCredits();
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function totalAmount(data) {
return data.performances
.reduce((total, p) => total + p.amount, 0);
}
function totalVolumeCredits(data) {
data.performances
.reduce((total, p) => total + p.volumeCredits, 0);
}
...
}- ์ด๋ฒ ์์ ์ผ๋ก ์ฐ๊ทน ์ฅ๋ฅด๋ณ ๊ณ์ฐ ์ฝ๋๋ค์ ํจ๊ป ๋ฌถ์๋ค.
- ์๋ก์ด ์ฅ๋ฅด๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด ์๋ธํด๋์ค ์์ฑ ํ ํฉํ ๋ฆฌ ํจ์์ ์ถ๊ฐ๋ง ํ๋ฉด ๋๋ค.
๋ง์น๋ฉฐ
- ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฒ์ ์ ์ฉํด๋ดค๋ค.
- ํจ์ ์ถ์ถํ๊ธฐ: ๋ก์ง์ ์ผ๋จ ๊ทธ๋๋ก ๋ผ์ ํจ์๋ก ๋บ
- ๋ณ์ ์ธ๋ผ์ธ ํ๊ธฐ: ์์ ๋ณ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์๊ณ ํจ์ ์์ฒด๋ฅผ ํธ์ถํ๋๋ก ๋ณ๊ฒฝ
- ํจ์ ์ฎ๊ธฐ๊ธฐ: ๋ ์ข์ ์์น์ ๋ชจ๋๋ก ์ด๋
- ์กฐ๊ฑด๋ถ ๋ก์ง์ ๋คํ์ฑ์ผ๋ก ๋ฐ๊พธ๊ธฐ
- ์ข์ ์ฝ๋๋ฅผ ๊ฐ๋ ํ๋ ํ์คํ ๋ฐฉ๋ฒ์ ์ผ๋ง๋ ์์ ํ๊ธฐ ์ฌ์ด๊ฐ์ด๋ค.
- ์ด๋ฒ ์์๋ฅผ ํตํด ๋ฆฌํฉํฐ๋ง ํ๋ ๋ฆฌ๋ฌ์ ๋ฐฐ์ ์ผ๋ฉด ํ๋ค.
- ๋จ๊ณ๋ฅผ ์๊ฐ๋ณด๋ค ๊ต์ฅํ ์๊ฒ ๋๋์๋ค.
- ์ฌ๋ฌ๊ฐ์ ๋จ๊ณ๋ฅผ ๋๊ธฐ๊ณ ํ๋ฒ์ ์ฒ๋ฆฌํ๋ ค๋ ์ ํน์ ์ค์ด๊ณ , ๋จ์ํ๊ฒ ์ฒ๋ฆฌํ๋ค.
- ์คํ๋ ค ๋ ์๊ฒ ๋๋ ์ผ ๋นจ๋ฆฌ ์ฒ๋ฆฌํ ์ ์๋ค.
- ์ด ์์ ๋จ๊ณ๋ค์ด ๋ชจ์ฌ ํฐ ๋ณํ๋ฅผ ๊ฐ์ ธ์จ๋ค. ์ด ์ ์ ๋ช ์ฌํ์.