Adapter Pattern

Adapter Pattern์€ ํ•œ ํด๋ž˜์Šค์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์–ด๋Žํ„ฐ ํŒจํ„ด์„ ์ด์šฉํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ํด๋ž˜์Šค๋“ค์„ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค.

Adapter Pattern์€ ๊ตฌ์กฐ ํŒจํ„ด์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋‚˜ ๊ตฌํ˜„์„ ๋ณตํ•ฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๊ฐ์ฒด๋ฅผ ํ•ฉ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. ์ด๋Š” ์ปดํŒŒ์ผ ๋‹จ๊ณ„์—์„œ๊ฐ€ ์•„๋‹Œ ๋Ÿฐํƒ€์ž„ ๋‹จ๊ณ„์—์„œ ๋ณตํ•ฉ ๋ฐฉ๋ฒ•์ด๋‚˜, ๋Œ€์ƒ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์œ ์—ฐ์„ฑ์„ ๊ฐ€์ง„๋‹ค.

์–ด๋Žํ„ฐ ํŒจํ„ด์€ Class Adapter์™€ Object Adapter ๋ฐฉ์‹์ด ์žˆ๋‹ค.

  • Class Adapter : ์ž๋ฐ” ์ƒ์†(inheritance)์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

  • Object Adapter : ์ž๋ฐ” ํ•ฉ์„ฑ(Composite)์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

์˜ˆ์ œ

  • volt ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” POJO ํด๋ž˜์Šค

public class Volt {

    private int volts;

    public Volt(int v){
        this.volts=v;
    }

    public int getVolts() {
        return volts;
    }

    public void setVolts(int volts) {
        this.volts = volts;
    }

}
  • Socket : 120 Volt ์ƒ์„ฑ ํด๋ž˜์Šค

public class Socket {

    public Volt getVolt(){
        return new Volt(120);
    }
}
  • 120 ๋ณผํŠธ ์™ธ์˜ ์ถ”๊ฐ€์ ์œผ๋กœ 3, 12 volt๋„ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑํ•˜๋Š” ์–ด๋Žํ„ฐ

public interface SocketAdapter {

    public Volt get120Volt();

    public Volt get12Volt();

    public Volt get3Volt();
}
  • Class Adapter ๋ฐฉ์‹

public class SocketClassAdapterImpl extends Socket implements SocketAdapter{

    @Override
    public Volt get120Volt() {
        return getVolt();
    }

    @Override
    public Volt get12Volt() {
        Volt v= getVolt();
        return convertVolt(v,10);
    }

    @Override
    public Volt get3Volt() {
        Volt v= getVolt();
        return convertVolt(v,40);
    }

    private Volt convertVolt(Volt v, int i) {
        return new Volt(v.getVolts()/i);
    }

}
  • Object Adapter ๋ฐฉ์‹

public class SocketObjectAdapterImpl implements SocketAdapter{

    //Using Composition for adapter pattern
    private Socket sock = new Socket();

    @Override
    public Volt get120Volt() {
        return sock.getVolt();
    }

    @Override
    public Volt get12Volt() {
        Volt v= sock.getVolt();
        return convertVolt(v,10);
    }

    @Override
    public Volt get3Volt() {
        Volt v= sock.getVolt();
        return convertVolt(v,40);
    }

    private Volt convertVolt(Volt v, int i) {
        return new Volt(v.getVolts()/i);
    }
}
  • main

public static void main(String[] args) {
           // object adapter
        SocketAdapter sockAdapter = new SocketObjectAdapterImpl();
        Volt v3 = sockAdapter.get3Volt();
        Volt v12 = sockAdapter.get12Volt();
        Volt v120 = sockAdapter.get120Volt();

          // class adapter
          SocketAdapter sockAdapter = new SocketClassAdapterImpl();
        Volt v3 = sockAdapter.get3Volt();
        Volt v12 = sockAdapter.get12Volt();
        Volt v120 = sockAdapter.get120Volt();
}

์ฐธ๊ณ 

Last updated