디자인패턴 3. Adapter Pattern

min seung moon·2021년 6월 25일
0

Spring

목록 보기
8/50
post-thumbnail

1. Adapter Pattern

  • Adapter는 실생활에서는 110v를 220v로 변경해주거나, 그 반대로 해주는 흔히 돼지코 라고 불리는 변환를 예로 들 수 있다
  • 호환성이 없는 기존 클래스의 인터페이스를 변환하여 재상요할 수 있도록 한다
  • SOLID 중에서 개방폐쇄 원칙(OCP)를 따른다

2. 프로젝트 테스트

01. 기존 프로젝트에서 테스트

  • JAVA Project, design
  • adapter Package 생성
  • Electronic110V, Electronic220V Interface 생성
  • HairDryer, AirConditional, Cleaner Class 생성
  • Electronic110V.java
package com.company.design.adapter;

public interface Electronic110V {
    void powerOn();
}
  • Electronic220V.java
package com.company.design.adapter;

public interface Electronic220V {
    void connect();
}
  • HairDryer.java
package com.company.design.adapter;

public class HairDryer implements Electronic110V{

    @Override
    public void powerOn() {
        System.out.println("헤어 드라이기 110v on");
    }
}
  • AirConditional.java
package com.company.design.adapter;

public class AirConditiional implements Electronic220V{

    @Override
    public void connect() {
        System.out.println("에어컨 220v on");
    }
}
  • Cleaner.java
package com.company.design.adapter;

public class Cleaner implements Electronic220V{
    @Override
    public void connect() {
        System.out.println("청소기 220v on");
    }
}
  • Main.java 작성
package com.company.design;

import com.company.design.adapter.Cleaner;
import com.company.design.adapter.Electronic110V;
import com.company.design.adapter.HairDryer;
import com.company.design.singleton.AClazz;
import com.company.design.singleton.BClazz;
import com.company.design.singleton.SocketClient;

public class Main {

    public static void main(String[] args) {
        /*
        AClazz aClazz = new AClazz();
        BClazz bClazz = new BClazz();

        SocketClient aClient = aClazz.getSocketClient();
        SocketClient bClient = bClazz.getSocketClient();

        System.out.println("두개의 객체가 동일한가?");
        System.out.println(aClient.equals(bClient));
         */

        HairDryer hairDryer = new HairDryer();
        connect(hairDryer);

        Cleaner cleaner = new Cleaner();
        connect(cleaner);
    }

    // 콘센트
    public static void connect(Electronic110V electronic110V) {
        electronic110V.powerOn();
    }
}

  • SocketAdapter.java
    • 220v를 110v로 변경시켜줄 어뎁터
package com.company.design.adapter;

public class SocketAdapter implements Electronic110V{

    private Electronic220V electronic220V;

    public SocketAdapter(Electronic220V electronic220V) {
        this.electronic220V = electronic220V;
    }

    @Override
    public void powerOn() {
        electronic220V.connect();
    }
}
  • Main.java
package com.company.design;

import com.company.design.adapter.*;
import com.company.design.singleton.AClazz;
import com.company.design.singleton.BClazz;
import com.company.design.singleton.SocketClient;

public class Main {

    public static void main(String[] args) {
        /*
        AClazz aClazz = new AClazz();
        BClazz bClazz = new BClazz();

        SocketClient aClient = aClazz.getSocketClient();
        SocketClient bClient = bClazz.getSocketClient();

        System.out.println("두개의 객체가 동일한가?");
        System.out.println(aClient.equals(bClient));
         */

        HairDryer hairDryer = new HairDryer();
        connect(hairDryer);

        Cleaner cleaner = new Cleaner();
        //connect(cleaner);
        Electronic110V adapter = new SocketAdapter(cleaner);
        connect(adapter);

        AirConditiional airConditiional = new AirConditiional();
        //connect(airConditiional);
        Electronic110V airAdapter = new SocketAdapter(airConditiional);
        connect(airAdapter);
    }

    // 콘센트
    public static void connect(Electronic110V electronic110V) {
        electronic110V.powerOn();
    }
}


profile
아직까지는 코린이!

0개의 댓글