Java

[Effective Java] ์•„์ดํ…œ 50. ์ ์‹œ์— ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค๋ผ.

quedevel 2023. 3. 24. 09:15
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 50. ์ ์‹œ์— ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค๋ผ.

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ถˆ๋ณ€์‹์„ ๊นจ๋œจ๋ฆฌ๋ ค ํ˜ˆ์•ˆ์ด ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋ฐฉ์–ด์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•ด์•ผ ํ•œ๋‹ค.


* ๊ธฐ๊ฐ„์„ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค - ๋ถˆ๋ณ€์‹์„ ์ง€ํ‚ค์ง€ ๋ชปํ–ˆ๋‹ค.
public final class Period {
    private final Date start;
    private final Date end;

    /**
     * @param  start ์‹œ์ž‘ ์‹œ๊ฐ
     * @param  end ์ข…๋ฃŒ ์‹œ๊ฐ. ์‹œ์ž‘ ์‹œ๊ฐ๋ณด๋‹ค ๋’ค์—ฌ์•ผ ํ•œ๋‹ค.
     * @throws IllegalArgumentException ์‹œ์ž‘ ์‹œ๊ฐ์ด ์ข…๋ฃŒ ์‹œ๊ฐ๋ณด๋‹ค ๋Šฆ์„ ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.
     * @throws NullPointerException start๋‚˜ end๊ฐ€ null์ด๋ฉด ๋ฐœ์ƒํ•œ๋‹ค.
     */
    public Period(Date start, Date end) {
        if (start.compareTo(end) > 0)
            throw new IllegalArgumentException(
                    start + "๊ฐ€ " + end + "๋ณด๋‹ค ๋Šฆ๋‹ค.");
        this.start = start;
        this.end = end;
    }

    public Date start() {
        return start;
    }

    public Date end() {
        return end;
    }

    public String toString() {
        return start + " - " + end;
    }
}    

์–ผํ• ์ด ํด๋ž˜์Šค๋Š” ๋ถˆ๋ณ€์ฒ˜๋Ÿผ ๋ณด์ด๊ณ , ์‹œ์ž‘ ์‹œ๊ฐ์ด ์ข…๋ฃŒ ์‹œ๊ฐ๋ณด๋‹ค ๋Šฆ์„ ์ˆ˜ ์—†๋‹ค๋Š” ๋ถˆ๋ณ€์‹์ด ๋ฌด๋ฆฌ ์—†์ด ์ง€์ผœ์งˆ ๊ฒƒ ๊ฐ™๋‹ค.

ํ•˜์ง€๋งŒ Date๊ฐ€ ๊ฐ€๋ณ€์ด๋ผ๋Š” ์‚ฌ์‹ค์„ ์ด์šฉํ•˜๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ๊ทธ ๋ถˆ๋ณ€์‹์„ ๊นจ๋œจ๋ฆด ์ˆ˜ ์žˆ๋‹ค.


  • Period ์ธ์Šคํ„ด์Šค์˜ ๋‚ด๋ถ€๋ฅผ ๊ณต๊ฒฉํ•ด๋ณด์ž.
Date start = new Date();
Date end = new Date();
Period p = new Period(start,end);
end.setYear(78); // p์˜ ๋‚ด๋ถ€๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค.

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

  • ์ˆ˜์ •ํ•œ ์ƒ์„ฑ์ž - ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ ๋‹ค.
public Period(Date start, Date end) {
    this.start = new Date(start.getTime());
    this.end   = new Date(end.getTime());
    if (this.start.compareTo(this.end) > 0)
        throw new IllegalArgumentException(
                this.start + "๊ฐ€ " + this.end + "๋ณด๋‹ค ๋Šฆ๋‹ค.");
}

๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๊ธฐ ์ „์— ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค๊ณ , ์ด ๋ณธ์‚ฌ๋ณธ์œผ๋กœ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•œ ์ ์„ ์ฃผ๋ชฉํ•˜์ž.

์ƒ์„ฑ์ž๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ์•ž์„œ์˜ ๊ณต๊ฒฉ์€ ๋ง‰์•„๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ, Period ์ธ์Šคํ„ด์Šค๋Š” ์•„์ง๋„ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๊ฐ€ ๋‚ด๋ถ€์˜ ๊ฐ€๋ณ€ ์ •๋ณด๋ฅผ ์ง์ ‘ ๋“œ๋Ÿฌ๋‚ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ์ˆ˜์ •ํ•œ ์ ‘๊ทผ์ž - ํ•„๋“œ์˜ ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
public Date start() {
    return new Date(start.getTime());
}
public Date end() {
    return new Date(end.getTime());
}

ํด๋ž˜์Šค๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›๋Š” ํ˜น์€ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ๊ฐ€๋ณ€์ด๋ผ๋ฉด

๊ทธ ์š”์†Œ๋Š” ๋ฐ˜๋“œ์‹œ ๋ฐฉ์–ด์ ์œผ๋กœ ๋ณต์‚ฌํ•ด์•ผํ•œ๋‹ค.


์ฐธ๊ณ  ์ž๋ฃŒ

Joshua Bloch, ใ€ŽEffective Java 3/Eใ€, ๊ฐœ์•ž๋งต์‹œ ์˜ฎ๊น€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ์‚ฌ์ดํŠธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=

728x90
๋ฐ˜์‘ํ˜•