๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๐ŸŒˆ Chapter 4: ํ”„๋ผ์ด๋ฒ„์‹œ

public void testMultiplication() {
Dollar five = new Dollar(5);
Dollar product = five.times(2);
assertEquals(10, product.amount);
product = five.times(3);
assertEquals(15, product.amount);
}

์ฒซ ๋ฒˆ์งธ ๋‹จ์–ธ์„ Dollar์™€ Dollar๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์žฌ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

public void testMultiplication() {
Dollar five = new Dollar(5);
Dollar product = five.times(2);
assertEquals(new Dollar(10), product);
product = five.times(3);
assertEquals(15, product.amount);
}

์ด๊ฒŒ ๋” ์ข‹์•„๋ณด์ด๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ ๋‹จ์–ธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ณ ์ณ๋ณด์ž.

public void testMultiplication() {
Dollar five = new Dollar(5);
Dollar product = five.times(2);
assertEquals(new Dollar(10), product);
product = five.times(3);
assertEquals(new Dollar(15), product);
}

์ด์ œ ์ž„์‹œ ๋ณ€์ˆ˜์ธ product๋Š” ๋” ์ด์ƒ ์“ธ๋ชจ์—†์–ด ๋ณด์ธ๋‹ค. ์ธ๋ผ์ธ์‹œ์ผœ๋ณด์ž.

public void testMultiplication() {
Dollar five = new Dollar(5);
assertEquals(new Dollar(10), five.times(2));
assertEquals(new Dollar(15), five.times(3));
}

์ด ํ…Œ์ŠคํŠธ๋Š” ์ผ๋ จ์˜ ์˜คํผ๋ ˆ์ด์…˜์ด ์•„๋‹ˆ๋ผ ์ฐธ์ธ ๋ช…์ œ์— ๋Œ€ํ•œ ๋‹จ์–ธ๋“ค์ด๋ฏ€๋กœ ์šฐ๋ฆฌ์˜ ์˜๋„๋ฅผ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ์ด์•ผ๊ธฐํ•ด์ค€๋‹ค.

ํ…Œ์ŠคํŠธ๋ฅผ ๊ณ ์น˜๊ณ  ๋‚˜๋‹Œ ์ด์ œ Dollar์˜ amount ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š” Dollar ์ž์‹ ๋ฐ–์— ์—†๊ฒŒ ๋๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ€์ˆ˜๋ฅผ private์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

private int amount;

์ด์ œ ํ• ์ผ ๋ชฉ๋ก์˜ ๋˜ ๋‹ค๋ฅธ ํ•ญ๋ชฉ์„ ์ง€์šธ ์ˆ˜ ์žˆ๊ฒŒ ๋๋‹ค. ํ•˜์ง€๋งŒ ์œ„ํ—˜ํ•œ ์ƒํ™ฉ์„ ๋งŒ๋“ค์—ˆ๋‹ค๋Š” ์ ์— ์ฃผ๋ชฉํ•˜๋ผ. ๋งŒ์•ฝ ๋™์น˜์„ฑ ํ…Œ์ŠคํŠธ๊ฐ€ ๋™์น˜์„ฑ์— ๋Œ€ํ•œ ์ฝ”๋“œ๊ฐ€ ์ •ํ™•ํžˆ ์ž‘๋™ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ฒ€์ฆํ•˜๋Š” ๋ฐ ์‹คํŒจํ•œ๋‹ค๋ฉด, ๊ณฑํ•˜๊ธฐ ํ…Œ์ŠคํŠธ ์—ญ์‹œ ๊ณฑํ•˜๊ธฐ์— ๋Œ€ํ•œ ์ฝ”๋“œ๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๋ฐ ์‹คํŒจํ•œ๋‹ค๋ฉด, ๊ณฑํ•˜๊ธฐ ํ…Œ์ŠคํŠธ ์—ญ์‹œ ๊ณฑํ•˜๊ธฐ์— ๋Œ€ํ•œ ์ฝ”๋“œ๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ฒ€์ฆํ•˜๋Š” ๋ฐ ์‹คํŒจํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๊ฒƒ์€ TDD๋ฅผ ์ ๊ทน์ ์œผ๋กœ ๊ด€๋ฆฌํ•ด์•ผ ํ•  ์œ„ํ—˜ ์š”์†Œ๋‹ค. ์šฐ๋ฆฌ๋Š” ์™„๋ฒฝํ•จ์„ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๋ชจ๋“  ๊ฒƒ์„ ๋‘ ๋ฒˆ ๋งํ•จ์œผ๋กœ์จ(์ฝ”๋“œ์™€ ํ…Œ์ŠคํŠธ๋กœ ํ•œ ๋ฒˆ์”จ) ์ž์‹ ๊ฐ์„ ๊ฐ€์ง€๊ณ  ์ „์ง„ํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ๋งŒ ๊ฒฐํ•จ์˜ ์ •๋„๋ฅผ ๋‚ฎ์ถ”๊ธฐ๋ฅผ ํฌ๋งํ•  ๋ฟ์ด๋‹ค. ๋–„๋•Œ๋กœ ์šฐ๋ฆฌ์˜ ์ถ”๋ก ์ด ๋งž์ง€ ์•Š์•„์„œ ๊ฒฐํ•จ์ด ์†๊ฐ€๋ฝ ์‚ฌ์ด๋กœ ๋น ์ ธ๋‚˜๊ฐ€๋Š” ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ๋Š”์ง€์— ๋Œ€ํ•œ ๊ตํ›ˆ์„ ์–ป๊ณ  ๋‹ค์‹œ ์•ž์œผ๋กœ ๋‚˜์•„๊ฐ„๋‹ค. ๊ทธ ์ดํ›„์—๋Š” ์šฉ๊ฐํ•˜๊ฒŒ ํŽ„๋Ÿญ์ด๋Š” ์ดˆ๋ก ๋ง‰๋Œ€ ์•„๋ž˜์„œ ๋Œ€๋‹ดํ•˜๊ฒŒ ์•ž์œผ๋กœ ๋‚˜์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.