Combine Functions into Class, ์—ฌ๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ํด๋ž˜์Šค๋กœ ๋ฌถ๊ธฐ๋ฅผ ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

function base(aReading) {...}
function taxableCharge(aReading) {...}
function calculateBaseCharge(aReading) {...}
class Reading {
    base() {...}
    taxableCharge() {...}
    calculateBaseCharge() {...}
}

๋ฐฐ๊ฒฝ

  • ๊ณตํ†ต ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๊ธด๋ฐ€ํ•˜๊ฒŒ ์—ฎ์—ฌ์žˆ๋Š” ํ•จ์ˆ˜ ๋ฌด๋ฆฌ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํด๋ž˜์Šค๋กœ ๋ฌถ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  • ํด๋ž˜์Šค๋กœ ๋ฌถ์œผ๋ฉด ํ•จ์ˆ˜๋“ค์ด ๊ณต์œ ํ•˜๋Š” ๊ณตํ†ต ํ™˜๊ฒฝ์„ ๋ช…์‹œ์ ์œผ๋กœ ๋“œ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜๋Š” ์ธ์ˆ˜๋„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ํด๋ž˜์Šค ๋ง๊ณ  ๋ณ€ํ™˜ ํ•จ์ˆ˜๋กœ ๋ฌถ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š”๋ฐ, ์ด๋Š” ๋‹ค์Œ์— ์•Œ์•„๋ณด์ž.

์ ˆ์ฐจ

  1. ํ•จ์ˆ˜๋“ค์ด ๊ณต์œ ํ•˜๋Š” ๊ณตํ†ต ๋ฐ์ดํ„ฐ ๋ ˆ์ฝ”๋“œ๋ฅผ ์บก์Šํ™”ํ•œ๋‹ค.
    • ์ด๊ฒŒ ์•ˆ๋˜์–ด ์žˆ์œผ๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ๋ถ€ํ„ฐ ํ•œ๋‹ค.
  2. ๊ณตํ†ต ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜ ๊ฐ๊ฐ์„ ์ƒˆ ํด๋ž˜์Šค๋กœ ์˜ฎ๊ธด๋‹ค.
    • ์ด๋•Œ ๊ณตํ†ต ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ฉค๋ฒ„๋Š” ํ˜ธ์ถœ๋ฌธ์˜ ์ธ์ˆ˜์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.
  3. ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ๋กœ์ง์€ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•˜์—ฌ ํด๋ž˜์Šค๋กœ ์˜ฎ๊ธด๋‹ค.

์˜ˆ์‹œ

reading = {customer: "ivan", quantity: 10, month: 5, year: 2017};
 
// client 1
const aReading = acquireReading();
const baseCharge = baseRate(aReading.month, aReading.year) * aReading.quantity;
 
// client 2
const aReading = acquireReading();
const base = (baseRate(aReading.month, aReading.year) * aReading.quantity);
const taxableCharge = Math.max(0, base - taxThreshold(aReading.year));
 
// client 3
const aReading = acquireReading();
const basicChargeAmount = calculateBaseCharge(aReading);
 
function calculateBaseCharge(aReading) {
    return baseRate(aReading.month, aReading.year) * aReading.quantity;
}
  • 1, 2, 3์—์„œ ๊ธฐ๋ณธ ์š”๊ธˆ ๊ณ„์‚ฐ ๊ณต์‹์ด ๋˜‘๊ฐ™๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๊ณ  3์—์„œ ์‚ฌ์šฉํ•œ ์ตœ์ƒ์œ„ ์ฝ”๋“œ๋ฅผ 1, 2์—์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์˜ณ์„๊นŒ?
  • ๋ฐ์ดํ„ฐ ๊ทผ์ฒ˜์— ๋‘๋Š” ๊ฒŒ ๋งž๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๋ฉด ํด๋ž˜์Šค๋‹ค.
class Reading {
    constructor(data) {
        this._customer = data.customer;
        this._quantity = data.quantity;
        this._month = data.month;
        this._year = data.year;
    }
 
    get customer() {
        return this._customer;
    }
 
    get quantity() {
        return this._quantity;
    }
 
    get month() {
        return this._month;
    }
 
    get year() {
        return this._year;
    }
 
    get baseCharge() {
        return baseRate(this.month, this.year) * this.quantity;
    }
 
    get taxableCharge() {
        return Math.max(0, this.baseCharge - taxThreshold(this.year));
    }
}
 
// client 1
const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const baseCharge = aReading.baseCharge;
 
// client 2
const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const taxableCharge = aReading.taxableCharge;
 
// client 3
const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const taxableCharge = aReading.taxableCharge;

Reference