Introduce Parameter Object, ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

function amountInvoiced(startDate, endDate) {...}
function amountReceived(startDate, endDate) {...}
function amountOverdue(startDate, endDate) {...}
function amountInvoiced(aDateRange) {...}
function amountReceived(aDateRange) {...}
function amountOverdue(aDateRange) {...}

๋ฐฐ๊ฒฝ

  • ๋ฐ์ดํ„ฐ ํ•ญ๋ชฉ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ํ•จ๊ป˜ ๋ชฐ๋ ค๋‹ค๋‹ˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์ฃผ์ž.
  • ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆ˜๊ฐ€ ์ค„์–ด๋“ค๊ณ , ์ผ๊ด€์„ฑ๋„ ๋†’์•„์ง„๋‹ค.
  • ํ•˜์ง€๋งŒ ๊ฐ€์žฅ ๊ทผ๋ณธ์ ์ธ ๊ฒƒ์€ ์ฝ”๋“œ๊ฐ€ ๋” ๊ทผ๋ณธ์ ์œผ๋กœ ๋ฐ”๋€๋‹ค๋Š” ์ ์— ์žˆ๋‹ค.

์ ˆ์ฐจ

  1. ์ ๋‹นํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๊ฐ€ ์—†๋‹ค๋ฉด ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.
    • Value Object๋กœ ๋งŒ๋“ค์ž.
  2. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  3. ํ•จ์ˆ˜ ์„ ์–ธ ๋ฐ”๊พธ๊ธฐ๋กœ ์ƒˆ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.
  4. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  5. ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ƒˆ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์ธ์Šคํ„ด์Šค๋ฅผ ๋„˜๊ธฐ๋„๋ก ์ˆ˜์ •ํ•œ๋‹ค. ๋ฐ”๊ฟ€๋•Œ ๋งˆ๋‹ค ํ…Œ์ŠคํŠธ
  6. ๊ธฐ์กด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ์ฝ”๋“œ๋ฅผ ์ƒˆ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ฐ”๊พผ๋‹ค.
  7. ๋‹ค ๋ฐ”๊ฟจ๋‹ค๋ฉด ๊ธฐ์กด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•œ๋‹ค.

์˜ˆ์‹œ

const station = {
    name: "ZB1",
    readings: [
        {temp: 47, time: "2016-11-10 09:10"},
        {temp: 53, time: "2016-11-10 09:20"},
        {temp: 58, time: "2016-11-10 09:30"},
        {temp: 53, time: "2016-11-10 09:40"},
        {temp: 51, time: "2016-11-10 09:50"},
    ]
};
 
function readingsOutsideRange(station, min, max) {
    return station.readings.filter(r => r.temp < min || r.temp > max);
}
 
alerts = readingsOutsideRange(station, operatingPlan.temperatureFloor, operatingPlan.temperatureCeiling);
  • operatingPlan์— ์žˆ๋Š” ๋ฒ”์œ„ ๊ฐ’์„ ๊ฐ€์ ธ์™€ ์ „๋‹ฌํ•˜๊ณ  ์žˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋ฒ”์œ„๋ผ๋Š” ๊ฐœ๋…์€ ๊ฐ์ฒด ํ•˜๋‚˜๋กœ ๋ฌถ์–ด ํ‘œํ˜„ํ•˜๋Š”๊ฒŒ ๋‚ซ๋‹ค.
class NumberRange {
    constructor(min, max) {
        this._data = {min: min, max: max};
    }
 
    get min() {
        return this._data.min;
    }
 
    get max() {
        return this._data.max;
    }
}
  • ๊ฐ’ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๋ ค ํ–ˆ์œผ๋‚˜, ์ด ๊ฒฝ์šฐ๋Š” ๋™์ž‘๊นŒ์ง€ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์•„ class๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.
  • ๋‹ค๋งŒ ๊ฐ’ ์ˆ˜์ • ์ž์ฒด๋Š” ์—†์„ ์˜ˆ์ •์ด๋ฏ€๋กœ setter๋Š” ๋งŒ๋“ค์ง€ ์•Š์•˜๋‹ค.
function readingsOutsideRange(station, min, max, range) {
    return station.readings.filter(r => r.temp < min || r.temp > max);
}
 
alerts = readingsOutsideRange(station, operatingPlan.temperatureFloor, operatingPlan.temperatureCeiling, null);
  • ์ผ๋‹จ ์ƒˆ๋กœ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ถ”๊ฐ€ํ•˜์ž.
  • ํ…Œ์ŠคํŠธ ํ•œ๋‹ค.
function readingsOutsideRange(station, min, max, range) {
    return station.readings.filter(r => r.temp < min || r.temp > max);
}
 
const range = new NumberRange(operatingPlan.temperatureFloor, operatingPlan.temperatureCeiling);
alerts = readingsOutsideRange(station, operatingPlan.temperatureFloor, operatingPlan.temperatureCeiling, range);
  • ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ƒˆ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์ธ์Šคํ„ด์Šค๋ฅผ ๋„˜๊ธฐ๋„๋ก ์ˆ˜์ •ํ•œ๋‹ค.
  • ๋™์ž‘์€ ์•ˆ๋ฐ”๊ฟจ์œผ๋‹ˆ ์ž˜ ๋  ๊ฒƒ์ด๋‹ค.
  • ํ…Œ์ŠคํŠธ.
function readingsOutsideRange(station, min, range) {
    return station.readings.filter(r => r.temp < min || r.temp > range.max);
}
 
const range = new NumberRange(operatingPlan.temperatureFloor, operatingPlan.temperatureCeiling);
alerts = readingsOutsideRange(station, operatingPlan.temperatureFloor, range);
  • max๋ถ€ํ„ฐ ๋Œ€์ฒดํ•ด๋ณด์ž.
function readingsOutsideRange(station, range) {
    return station.readings.filter(r => r.temp < min || r.temp > range.max);
}
 
const range = new NumberRange(operatingPlan.temperatureFloor, operatingPlan.temperatureCeiling);
alerts = readingsOutsideRange(station, range);
  • ์ด์ œ ๋๋‚ฌ๋‹ค.

์ง„์ •ํ•œ ๊ฐ’ ๊ฐ์ฒด๋กœ ๊ฑฐ๋“ญ๋‚˜๊ธฐ

  • ๊ฐ’ ๊ฐ์ฒด๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—… ์ž์ฒด๋Š” ๊ฐ’์ง„ ์ž‘์—…์˜ ์ค€๋น„๋‹จ๊ณ„๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๊ฐ’ ๊ฐ์ฒด๋กœ ๋งŒ๋“  ๊ฐ์ฒด์˜ ๊ด€๋ จ ๋™์ž‘์„ ์ด ํด๋ž˜์Šค๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด ์˜จ๋„๊ฐ€ ํ—ˆ์šฉ ๋ฒ”์œ„ ๋‚ด์ธ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.
class NumberRange {
    constructor(min, max) {
        this._data = {min: min, max: max};
    }
 
    get min() {
        return this._data.min;
    }
 
    get max() {
        return this._data.max;
    }
 
    function contains(arg) {
        return (arg >= this.min && arg <= this.max);
    }
}
 
function readingsOutsideRange(station, range) {
    return station.readings.filter(r => !range.contains(r.temp));
}
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฒ”์œ„๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ•œ ๊ณณ์œผ๋กœ ๋ชจ์„ ์ˆ˜ ์žˆ๋‹ค.

Reference