92. 디자인 패턴/구조(Structral)

01. 자바 어댑터 패턴 (JAVA Adapter Pattern)

THE HEYDAZE 2020. 6. 19. 04:08

# 참고한 사이트
 

Adapter Pattern: 어댑터 패턴

어댑터처럼 어떤 인터페이스를 클라이언트에서 요구하는 형태의 인터페이스에 적응시켜주는 역할을 하는 패...

blog.naver.com

 

# 설명

한 클래스의 인터페이스를 클라이언트에서 사용하고자 하는 다른 인터페이스로 변환 합니다

어댑터를 이용하면 인터페이스 호환성 문제 때문에 같이 쓸 수 없는 클래스들을 연결해서 쓸 수 있습니다

 

A 인터페이스 - A Impl 클래스(인스턴스 변수(멤버 변수) = B 인터페이스) - B Impl 클래스

 

예시)

220V > 110V 변환 어댑터 > 100V

 

# 장점

관계가 없는 인터페이스 간 같이 사용가능

프로그램 검사 용이

클래스 재활용성이 증가

 

# 사용 여부

[출처] https://blog.naver.com/ubellog91/221346554138

 

# 사용 방법

매개변수 같은 A B 메소드를 찾는다

Adapter 클래스를 생성한다

기존 인터페이스를 implement 한다

변환 하고자 하는 인터페이스 또는 클래스를 멤버변수로 선언한다

기존 인터페이스 오버라이딩 A 메소드 안B 메소드를 적는다

 

# 구조

기능이 비슷해 보이는 클래스들을 연결해주는 패턴

 

# 요구 사항
# 변경 사항
# 결과

이미지 클릭

 

# Interface
1
2
3
4
5
6
7
8
/** 모양 */
public interface Shape {
    
    /** 그리기 */
    void draw(int x, int y, int z, int j);
    
}
 
cs

 

# Adapter Impl

LineAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class LineAdapter implements Shape {
    /** 선 */
    private Line adaptee;
 
    /** 생성자 */
    public LineAdapter(Line line) {
        this.adaptee = line;
    }
 
    /** 그리기 */
    @Override
    public void draw(int x1, int y1, int x2, int y2) {
        adaptee.draw(x1, y1, x2, y2);
    }
 
    @Override
    public String toString() {
        return "LineAdapter";
    }
}
 
cs

 

RectangleAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/** 직사각형 어댑터 */
public class RectangleAdapter implements Shape {
 
    /** 직사각형 */
    private Rectangle adaptee;
 
    /** 생성자 */
    public RectangleAdapter(Rectangle rectangle) {
        this.adaptee = rectangle;
    }
 
    /** 그리기 */
    @Override
    public void draw(int x1, int y1, int x2, int y2) {
        int x = Math.min(x1, x2);
        int y = Math.min(y1, y2);
        int width = Math.abs(x2 - x1);
        int height = Math.abs(y2 - y1);
        adaptee.draw(x, y, width, height);
    }
 
    @Override
    public String toString() {
        return "RectangleAdapter";
    }
}
 
cs

 

# Adaptee

Line.java

1
2
3
4
5
6
7
8
9
10
/** 선 */
public class Line implements Shape {
 
    /** 그리기 */
    @Override
    public void draw(int x1, int y1, int x2, int y2) {
        System.out.println("Line -> 좌표A: (" + x1 + ";" + y1 + "), 좌표B: (" + x2 + ";" + y2 + ")");
    }
}
 
cs

 

Rectangle.java

1
2
3
4
5
6
7
8
9
10
11
12
/** 직사각형 */
public class Rectangle implements Shape {
 
    /** 그리기 */
    @Override
    public void draw(int x, int y, int width, int height) {
 
        System.out.println("Rectangle -> 좌표: (" + x + ";" + y + "), 넓이: " + width
                + ", 높이: " + height);
    }
}
 
cs

 

# Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** 사용자 */
public class Client {
 
    public static void main(String[] args) {
 
        // 선과 직사각형 어댑터
        Shape[] shapes = {new RectangleAdapter(new Rectangle()), new LineAdapter(new Line())};
 
        System.out.println("shape[0] = " + shapes[0].toString());
        System.out.println("shape[1] = " + shapes[1].toString());
        System.out.println();
 
        // Line.draw(x1,y1,x2,y2)
        // Rectangle.draw(x1,y1,width,height)
        int x1 = 10, y1 = 20;
        int x2 = 30, y2 = 60;
 
        for (Shape shape : shapes) {
            shape.draw(x1, y1, x2, y2);
        }
    }
}
 
cs
# Diagram