Adapter Pattern

์ตœ์ค€ํ˜ธยท2022๋…„ 11์›” 27์ผ
0

Design Pattern

๋ชฉ๋ก ๋ณด๊ธฐ
7/7
post-thumbnail

๐Ÿ“— Adapter Pattern

Adapter Pattern์€ adaptee์™€ target์˜ ๊ฐœ๋…์„ ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๋ฉด ์‰ฌ์›Œ์ง€๋Š”๋ฐ ์ด ๋ถ€๋ถ„์„ ํ™•์‹คํžˆ ์ธ์ง€ํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ๋ณด์ž!

220V ์–ด๋Œ‘ํ„ฐ๋ฅผ 110V ์–ด๋Œ‘ํ„ฐ๋กœ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ํ•ด์ฃผ๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค.

๊ธฐ์กด์—๋Š” ํ˜ธํ™”๋˜์ง€ ์•Š๋˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ˜ธํ™˜์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ๋””์ž์ธ ํŒจํ„ด์ด๋‹ค.

๐Ÿ“„ ํŒจํ„ด ์ ์šฉ ์ „

โŒจ๏ธ ์‚ผ์„ฑ ์ถฉ์ „๊ธฐ

public interface SamsungCharging {
    void samsungCharging();
}
public class SamsungChargingImpl implements SamsungCharging{
    @Override
    public void samsungCharging() {
        System.out.println("์‚ผ์„ฑ ํ•ธ๋“œํฐ ์ถฉ์ „ ์‹œ์ž‘");
    }
}

โŒจ๏ธ ์• ํ”Œ ์ถฉ์ „๊ธฐ

public interface AppleCharging {
    void appleCharging();
}
public class AppleChargingImpl implements AppleCharging{
    @Override
    public void appleCharging() {
        System.out.println("์• ํ”Œ ํ•ธ๋“œํฐ ์ถฉ์ „ ์‹œ์ž‘");
    }
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ ์‚ผ์„ฑ๊ณผ ์• ํ”Œ ์ถฉ์ „๊ธฐ๊ฐ€ ์žˆ๋‹ค. ํ•ด๋‹น ์ถฉ์ „๊ธฐ๋“ค์€ ์„œ๋กœ ํ˜ธํ™˜๋˜์ง€ ์•Š์œผ๋ฉฐ ๊ฐ์ž samsungCharging appleCharging ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ถฉ์ „์„ ์‹œ์ž‘ํ•œ๋‹ค.

public class Client {
    public static void main(String[] args) {
        SamsungCharging samsung = new SamsungChargingImpl();
        samsung.samsungCharging();

        AppleCharging apple = new AppleChargingImpl();
        apple.appleCharging();
    }
}

์‚ฌ์šฉ์ž๋Š” ๊ฐ๊ฐ์˜ ์ถฉ์ „๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๊ฐ๊ฐ ์ถฉ์ „์„ ์‹œ์ž‘ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์• ํ”Œ ์ถฉ์ „๊ธฐ๊ฐ€ ์ถฉ์ฒœ ํฌํŠธ ํ†ตํ•ฉ์œผ๋กœ ์ธํ•ด ์‚ผ์„ฑ ์ถฉ์ „๊ธฐ๋กœ๋„ ์ถฉ์ „์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ๋‹ค.

๐Ÿ“„ ํŒจํ„ด ์ ์šฉ

public class AppleChargingAdapter implements SamsungCharging {
    private AppleCharging appleCharging;

    public AppleChargingAdapter(AppleCharging appleCharging) {
        this.appleCharging = appleCharging;
    }

    @Override
    public void samsungCharging() {
        appleCharging.appleCharging();
    }
}

AppleChargingAdapter๋Š” target์ด ๋˜๋Š” SamsungCharging์„ ์ƒ์†ํ•˜๊ณ  adaptee๊ฐ€ ๋˜๋Š” AppleCharging๋ฅผ ์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ผ์„ฑ ์ถฉ์ „๊ธฐ๋กœ๋„ ์• ํ”Œ์„ ์ถฉ์ „ํ•  ์ˆ˜ ์žˆ๋„๋ก ์‹คํ–‰ํ•ด์ฃผ๋ฉด

public class Client {
    public static void main(String[] args) {
        AppleCharging apple = new AppleChargingImpl();
        AppleChargingAdapter adapter = new AppleChargingAdapter(apple);
        adapter.samsungCharging();
    }
}

์ด๋ ‡๊ฒŒ adaptee์ธ Apple์˜ ์ถฉ์ „์„ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ๋ฌธ๊ตฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๊ฐ€ ์ด ํŒจํ„ด์˜ ํŠน์ง•์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ๊ธฐ์กด์˜ AppleCharging์˜ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์•˜์ง€๋งŒ(= ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š” OCP) Adapter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ samsungCharging ์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

profile
ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์ด์ „ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. https://ililil9482.tistory.com

0๊ฐœ์˜ ๋Œ“๊ธ€