Java

[Effective Java] ์•„์ดํ…œ 24. ๋งด๋ฒ„ ํด๋ž˜์Šค๋Š” ๋˜๋„๋ก static์œผ๋กœ ๋งŒ๋“ค๋ผ.

quedevel 2023. 3. 20. 10:33
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 24. ๋งด๋ฒ„ ํด๋ž˜์Šค๋Š” ๋˜๋„๋ก static์œผ๋กœ ๋งŒ๋“ค๋ผ.

  • ์˜ˆ์ œ ) ๋นŒ๋” ํŒจํ„ด
public class Member {
    private long mbrSn;
    private String mbrId;
    private String mbrPw;
    private String mbrNm;

    public Member(Builder builder) {
        this.mbrSn = builder.mbrSn;
        this.mbrId = builder.mbrId;
        this.mbrPw = builder.mbrPw;
        this.mbrNm = builder.mbrNm;
    }

    public static Builder builder(String id, String pw) {
        return new Builder(id, pw);
    }

    public static class Builder {
        public long mbrSn;
        public String mbrId;
        public String mbrPw;
        public String mbrNm;

        private Builder(String mbrId, String mbrPw) {
            this.mbrId = mbrId;
            this.mbrPw = mbrPw;
        }

        public Builder with(Consumer<Builder> consumer) {
            consumer.accept(this);
            return this;
        }

        public Member build() {
            return new Member(this);
        }
    }

    /* getter ์ƒ๋žต */
    ...
}   

์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค(Builder)๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค ์•ˆ์— ์„ ์–ธ๋˜๊ณ , ๋ฐ”๊นฅ ํด๋ž˜์Šค(Member)์˜ private ๋งด๋ฒ„์—๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ๋งŒ ์ œ์™ธํ•˜๊ณ ๋Š” ์ผ๋ฐ˜ ํด๋ž˜์Šค์™€ ๋˜‘๊ฐ™๋‹ค. ์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค์™€ ๋น„์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค์˜ ๊ตฌ๋ฌธ์ƒ ์ฐจ์ด๋Š” ๋‹จ์ง€ static์ด ๋ถ™์–ด ์žˆ๊ณ  ์—†๊ณ  ๋ฟ์ด์ง€๋งŒ, ์˜๋ฏธ์ƒ ์ฐจ์ด๋Š” ์˜์™ธ๋กœ ๊ฝค ํฌ๋‹ค. ๋น„์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์™€ ์•”๋ฌต์ ์œผ๋กœ ์—ฐ๊ฒฐ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ๋น„์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ์—์„œ ์ •๊ทœํ™”๋œ this๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

โญ ๊ทธ๋ ‡๋‹ค๋ฉด ๋น„์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค๋Š” ์–ด๋””์— ์“ฐ์ด๋‚˜? - ๋น„์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค๋Š” ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ •์˜ํ•  ๋•Œ ์ž์ฃผ ์“ฐ์ธ๋‹ค.

์ฆ‰, ์–ด๋–ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ์‹ธ ๋งˆ์น˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒํ•˜๋Š” ๋ทฐ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


  • ๋น„์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค์˜ ํ”ํ•œ ์“ฐ์ž„ - ์ž์‹ ์˜ ๋ฐ˜๋ณต์ž ๊ตฌํ˜„

public class MySet<E> extends AbstractSet<E> {
    ... // ์ƒ๋žต

    @Override
    public Iterator<E> iterator() {
        return new MyIterator();
    }

    private class MyIterator implements Iterator<E> { ... }
}

๋งด๋ฒ„ ํด๋ž˜์Šค์—์„œ ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•  ์ผ์ด ์—†๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด static์„ ๋ถ™์—ฌ์„œ ์ •์  ๋งด๋ฒ„ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ž.

static์„ ์ƒ๋žตํ•˜๋ฉด ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค๋กœ์˜ ์ˆจ์€ ์™ธ๋ถ€ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค. ์ด ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๋ ค๋ฉด ์‹œ๊ฐ„๊ณผ ๊ณต๊ฐ„์ด ์†Œ๋น„๋œ๋‹ค.

๋” ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๋Š” ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์ด ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ˆ˜๊ฑฐํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.

โญ ํ•ต์‹ฌ ์ •๋ฆฌ

์ค‘์ฒฉ ํด๋ž˜์Šค์—๋Š” ๋„ค ๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ์˜ ์“ฐ์ž„์ด ๋‹ค๋ฅด๋‹ค. ๋ฉ”์„œ๋“œ ๋ฐ–์—์„œ๋„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ฑฐ๋‚˜ ๋ฉ”์„œ๋“œ ์•ˆ์— ์ •์˜ํ•˜๊ธฐ์—” ๋„ˆ๋ฌด ๊ธธ๋‹ค๋ฉด ๋งด๋ฒ„ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ ๋‹ค. ๋งด๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๊ฐ๊ฐ์ด ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•œ๋‹ค๋ฉด ๋น„์ •์ ์œผ๋กœ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ •์ ์œผ๋กœ ๋งŒ๋“ค์ž. ์ค‘์ฒฉ ํด๋ž˜์Šค๊ฐ€ ํ•œ ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ๋งŒ ์“ฐ์ด๋ฉด์„œ ๊ทธ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ง€์ ์ด ๋‹จ ํ•œ ๊ณณ์ด๊ณ  ํ•ด๋‹น ํƒ€์ž…์œผ๋กœ ์“ฐ๊ธฐ์— ์ ํ•ฉํ•œ ํด๋ž˜์Šค๋‚˜ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ด๋ฏธ ์žˆ๋‹ค๋ฉด ์ต๋ช… ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ง€์—ญ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ž.

์ฐธ๊ณ  ์ž๋ฃŒ


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
๋ฐ˜์‘ํ˜•