Program Language/java

์ธํ„ฐํŽ˜์ด์Šค ๋„ค์ด๋ฐ์€ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

๊ฐœ๋ฐœํ•˜๋Š” ์ฃผ๋””์”จ 2023. 10. 10. 12:45

 

๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด, service๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ  implementsํ•ด์„œ ์‚ฌ์šฉํ•  ๋•Œ๊ฐ€ ๋งŽ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ์–ด๋Š์ƒŒ๊ฐ€๋ถ€ํ„ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๋‹จ์ผ ๊ตฌํ˜„์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๊ธฐ๊ณ„์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์™”๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๋ฉฐ ํฌ์ŠคํŒ…์„ ์ •๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

 

์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด  Impl ์ ‘๋ฏธ์‚ฌ๋Š” ์ง€์–‘ํ•ด์•ผํ•œ๋‹ค

ํ—๊ฐ€๋ฆฌ์•ˆ ํ‘œ๊ธฐ๋ฒ•์˜ ์ž”์žฌ

ํ•™๊ต๋‚˜ ๋‹จ์ˆœํ•œ ์ฝ”๋“œ ๊ฐœ๋ฐœ ๋“ฑ ์ƒ๊ฐ๋ณด๋‹ค ๊ฝค ๋งŽ์€ ๊ณณ์—์„œ ์•„์ง๊นŒ์ง€ ์ด๋Ÿฐ layout์„ ๋ณธ ์ ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„์ฒด์—๋Š” Impl ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๋ถ™์€ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ํ—๊ฐ€๋ฆฌ์•ˆ ํ‘œ๊ธฐ๋ฒ•์˜ ์ž”์žฌ๋กœ, ์ง€์–‘ํ•ด์•ผ ํ•˜๋Š” ํ‘œ๊ธฐ๋ฒ•์ด๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค ๋„ค์ด๋ฐ ๋ฐฉ๋ฒ•

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋„ค์ด๋ฐํ•  ๋•Œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ด๋ฆ„์„ ๊ทธ ์˜๋ฏธ ์ž์ฒด์— ๋”ฐ๋ผ ์ง€์–ด์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ „์ง„go()), ํ›„์ง„(back()), ํ˜„์žฌ ์œ„์น˜ ๋ฐ˜ํ™˜(getDistance())์˜ ๊ธฐ๋Šฅ์„ ๊ณตํ†ต์œผ๋กœ ๊ฐ–๋Š” ์ž๋™์ฐจ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ํ•˜์ž.

๊ณตํ†ต ๊ธฐ๋Šฅ์ธ ์ „์ง„, ํ›„์ง„, ํ˜„์žฌ ์œ„์น˜ ๋ฐ˜ํ™˜์„ ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค๋“ค์ด ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์€ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ด์ง€ ๋ชปํ•œ ์ฝ”๋“œ์ด๋‹ค. ๊ฐ ๊ฐ์ฒด์—์„œ ์ค‘๋ณต๋˜๋Š” ์ „์ง„, ํ›„์ง„, ํ˜„์žฌ ์œ„์น˜ ๋ฐ˜ํ™˜์˜ ํ–‰์œ„๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™”ํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.

์ด๋•Œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋„ค์ด๋ฐ์€ ์ž๋™์ฐจ์˜ ์›€์ง์ž„์ด๋ผ๋Š” ํ–‰์œ„ ์ž์ฒด๋กœ ๋„ค์ด๋ฐ์„ ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

public class CarTest {

    public static void main(String[] args) {
        Move avante = new AvanteCar();
        Move porsche = new PorscheCar();
        avante.go();
        porsche.go();
        porsche.back();
        System.out.println("avanteCar = " + avante.getDistance());  // 10
        System.out.println("porscheCar = " + porsche.getDistance());  // 90
    }
}

interface Move {

    void go();

    void back();

    int getDistance();
}

class AvanteCar implements Move {

    private static final int GO_COUNT = 10;
    private static final int BACK_COUNT = 5;

    private int distance = 0;

    @Override
    public final int getDistance() {
        return this.distance;
    }

    @Override
    public final void go() {
        distance += GO_COUNT;
    }

    @Override
    public final void back() {
        distance -= BACK_COUNT;
    }
}

class PorscheCar implements Move {

    private static final int GO_COUNT = 100;
    private static final int BACK_COUNT = 10;

    private int distance = 0;

    @Override
    public final int getDistance() {
        return this.distance;
    }

    @Override
    public final void go() {
        distance += GO_COUNT;
    }

    @Override
    public final void back() {
        distance -= BACK_COUNT;
    }
}

์ถ”๊ฐ€๋กœ, Java Standard Library์—์„œ List ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์ธ ArrayList, LinkedList๋ฅผ ArrayListImpl, LinkedListImpl์ด๋ผ๊ณ  ๋ช…๋ช…ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ†ตํ•ด ํ—๊ฐ€๋ฆฌ์•ˆ ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Impl๊ณผ ๊ฐ™์€ ์ ‘๋ฏธ์–ด๋Š” DRY ๊ทœ์น™์—๋„ ์–ด๊ธ‹๋‚œ๋‹ค.

 

DRY ๊ทœ์น™

DRY ๊ทœ์น™์ด๋ž€

DRY ๊ทœ์น™์€ Don’t Repeat Yourself์˜ ์•ฝ์ž๋กœ, ‘๋ฐ˜๋ณตํ•˜์ง€ ๋งˆ๋ผ’๋Š” ๊ทœ์น™์ด๋‹ค.

DRY ๊ทœ์น™์— ์œ„๋ฐฐ๋˜๋Š” ๋„ค์ด๋ฐ ์‹ค์ˆ˜

์•ž์—์„œ ์–ธ๊ธ‰ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์— Impl ๊ณผ ๊ฐ™์€ ์ ‘๋ฏธ์‚ฌ๋ฅผ ๋ถ™์ด๋Š” ํ–‰์œ„์ฒ˜๋Ÿผ DTO(Data Transfer Object)์— dto ์ ‘๋ฏธ์‚ฌ๋ฅผ ๋ถ™์ด๋Š” ์‹ค์ˆ˜๋ฅผ ๋งŽ์ด ํ•œ๋‹ค. ๋‚˜ ๋˜ํ•œ ์ง€๊ธˆ๊นŒ์ง€ DTO์— ํ•ด๋‹น ์ ‘๋ฏธ์‚ฌ๋ฅผ ๋ถ™์˜€๋‹ค. dto์ ‘๋ฏธ์‚ฌ๋ฅผ ๋ถ™์ด๋Š” ๋Œ€์‹  ํŒจํ‚ค์ง€(package)์— ๋„ฃ์Œ์œผ๋กœ์จ DRY ๊ทœ์น™์„ ๋”ฐ๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

 

์ •๋ฆฌ

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

 

 

์ง€๊ธˆ๊นŒ์ง€ ๋‹น๋‹นํ•˜๊ฒŒ Impl์„ ๋ถ™์ธ ๋‚ด ๊ณผ๊ฑฐ๊ฐ€ ๋ถ€๋„๋Ÿฝ๋‹ค

 

https://velog.io/@minide/Java-์ธํ„ฐํŽ˜์ด์ŠคInterface-๋„ค์ด๋ฐ