스프링 입문을 위한 자바 객체지향의 원리와 이해 (Adapter Pattern)

1. 어댑터 패턴 (Adapter Pattern)

흔히 변환기라고도 부르는 패턴입니다
예를들면 서로 다른 충전기가 있습니다 휴대폰 충전기의 경우 휴대폰을 직접 전원 콘센트에 연결 할 수 없기 때문에 충전기가 핸드폰과 전원 콘센트 사이에서 둘을 연결해주는 변환기 역활을 합니다

즉 어댑터 패턴은 -> SOLID(OCP) 개방 페쇄 원칙이랑 같습니다


어댑터 패턴을 적용하기 전

public class ServiceA {

    void runServiceA(){
        System.out.println("A");
    }
}
public class ServiceB {

    void runServiceB(){
        System.out.println("B");
    }
}
public class Main {


    public static void main(String[] args) {



        //어댑터 패턴 적용 X
        ServiceA nota=new ServiceA();
        ServiceB notb=new ServiceB();

        nota.runServiceA()
        notb.runServiceB();


    }
}

어댑터 패턴을 적용한



public class AdapterA {
    ServiceA serviceA=new ServiceA();
    
    void run(){
        System.out.print("어댑터 패턴 적용 -> ");
        serviceA.runServiceA();
    }
}
public class AdapterB {
    ServiceB serviceB=new ServiceB();

    void run(){
        System.out.print("어댑터 패턴 적용 -> ");
        serviceB.runServiceB();
    }
}

public class Main {
    public static void main(String[] args) {


        //어댑터 패턴
        AdapterA a=new AdapterA();
        //어댑터 패턴 적용 X
        ServiceA nota=new ServiceA();

        AdapterB b=new AdapterB();
        ServiceB notb=new ServiceB();

        a.run();
        nota.runServiceA();

        b.run();
        notb.runServiceB();


    }
}

player1 ,player2 실행하는 소스코드입니다.

Adapter 클래스를 통해 변환을해서 출력하는걸 볼 수 있습니다.

public interface Action {

    /**
     *
     * 행동 인터페이스
     */
    void eat(String food);
    void attack(String weapon);
    void buy(String name);
}
public class player1  implements Action{


    @Override
    public void eat(String food) {
        System.out.println(food+"먹습니다.");
    }

    @Override
    public void attack(String weapon) {
        System.out.println(weapon+"공격!!");
    }

    @Override
    public void buy(String name) {
        System.out.println(name+"구입완료");
    }
}
public class Adapter implements Action{

    private Action action;

    public Adapter(Action action){
        this.action=action;
    }

    @Override
    public void eat(String food) {
        System.out.println("Using Adapter ---->");
        action.eat(food);
    }

    @Override
    public void attack(String weapon) {
        System.out.println("Using Adapter ---->");
        action.attack(weapon);
    }

    @Override
    public void buy(String name) {
        System.out.println("Using Adapter ---->");
        action.buy(name);
    }


}
public class player2 implements Action {


    @Override
    public void eat(String food) {
        System.out.println(food+"먹습니다.");
    }

    @Override
    public void attack(String weapon) {
        System.out.println(weapon+"공격!!.");
    }

    @Override
    public void buy(String name) {
        System.out.println(name+"구입합니다.");
    }
}
public class Main {


    public static void main(String[] args) {


        Adapter adapter1= new Adapter(new player1());

        adapter1.attack("화살");
        adapter1.eat("포션");

        Adapter adapter2 =new Adapter(new player2());
        adapter2.attack("칼");
        adapter2.eat("포션");
    }
}

토이 프로젝트에 적용해보기

기존코드에서는 StationRepostory,LineRepository에서 delete메서드를 불러와서 출력을 진행했습니다.
어댑터 패턴을 적용하고 나서 따로 FormatAdapter,FormatAdapterLine 클래스를 만들고 StationRepostory,LineRepository클래스에 있는 delete 메서드를 넣어주고 실행하는걸 볼 수 있습니다.


    private FormatAdapter formatAdapter=new FormatAdapter();
    private FormatAdapterLine formatAdapterLine=new FormatAdapterLine();

            else if(name.equals("2")){
                System.out.println("## 삭제할 역 이름을 입력하세요");
                name=sc.nextLine();
                formatAdapter.FormatDelete(name);
            }
            
    		else if(name.equals("2")){
                    System.out.println("## 삭제할 노선 이름을 입력하세요");
                    String linename=sc.nextLine();
                    formatAdapterLine.FormatDelete(linename);

                }
public class FormatAdapter{

    void FormatDelete(String name){
        StationRepository.deleteStation(name);
    }
}

public class FormatAdapterLine{

    
    public void FormatDelete(String name){
        LineRepository.deleteLineByName(name);
    }
}

좋은 웹페이지 즐겨찾기