Bridge Design [브리지 디자인]

  1. Design Object [디자인목적]

    1. 인터페이스와 구현 객체 사이에 결합도를 줄여 조금 더 유연하고 재사용 가능한 설계 가능
  2. Options [대안들]

    1. 인터페이스와 구현 객체 사이의 결합도가 높을 경우, 모듈화는 가능하지만, 모듈화 정도가 높아질 수록 같은 기능에 대해 반복적인 코드 작성을 해야한다.

      -> 확장성이 떨어지며, 기능이 추가되거나 변경 시 시스템 전반적으로 수정해야한다.

  3. Benefit [디자인 장점]

    1. 추상클래스를 통해서 다형성이 확보되므로 "인터페이스 - 구현" 의 강한 결합을 어느정도 느슨하게 만들 수 있다.

    2. 이를 통해 획득된 느슨한 결합을 통해 유연한 상속관계를 갖을 수 있다.

    3. Runtime 상황에서 객체에 대한 환경설정이 필요하다.

    4. 추상클래스 VS 인터페이스

      1. 추상클래스 : 일반적인 클래스와 달리, 간략화(추상화)된 속성들을 갖는다. 추상클래스는 속성, 일반메소드, 추상메소드를 갖을 수 있으며, 추상메소드는 추상클래스 내부에서만 선언 가능하다.
        추상클래스 자체로는 객체생성이 어려워 자식클래스에서 extends 키워드로 확장시켜줘야 한다.

      2. 인터페이스 : 추상클래스의 극단적인 경우. 추상메소드는 갖을 수 있으며, 서브클래스에서 implements 키워드로 선언가능하다. 서브클래스에서는 상속받은 인터페이스의 추상메소드를 반드시 재정의(override) 해야 한다.

  4. Structure & Sample Code [구조와 샘플코드]

    1) 서로다른 두 운영체제에서 돌아가는 어플리케이션의 화면 요소 셋

    1. UML

    2. 구조

      -> Window class는 WindowImpl abstarct를 멤버변수로 갖으며, setter또한 구현되엉
      -> IconWindow는 Window를 상속받으며, 클라이언트에서는 IconWindow클래스를 통해 LinuxWindowImpl과 DirectXWindowImpl을 생성할 수 있다.

    3. Bridge Design
      -> IconWindow와 Window가 다리(Bridge)가 되므로써, 다형성을 통해, 추가/변경이 잦은 LinuxWindowImpl과 DirectXWindowImpl의 직접 접근을 막는다.
      -> 클라이언트(app)는 IconWindow(Window)만 접근하면 된다.

    1)App(client)

public class App {

    public static void main(String[] args) {
        IconWindow iconWindow = new IconWindow();      // Bridge
        iconWindow.setWindow(new LinuxWindowImpl());   // Access LinuxWindow
        iconWindow.drawIcon();
        iconWindow.setWindow(new DirectXWindow());     // Access DirectXWindow
        iconWindow.drawIcon();
        iconWindow.draw(5, 5, 10, 100, "TEST");
    }

}

2) IconWindow

public class IconWindow extends Window{
    public void drawIcon(){
        draw(0,0,10,10,"white");
        draw(0,10,10,10,"");
    }
}

3) Window

public class Window {
    private WindowImpl window;   // Linux와 DirextX에 접근하기 위한 부모 객체 

    public void draw(int x, int y, int width, int height, String color){
        window.draw(x, y, width, height, color);
    }

    public void setWindow(WindowImpl window) {this.window = window;}  // 멤버변수의 setter


}

4) WindowImpl -> 추상클래스 선언을 통한 다형성 확

public abstract class WindowImpl {
    public abstract void draw(int x, int y, int width, int height, String color);

}

5) LinuxWindowImpl

public class LinuxWindowImpl extends WindowImpl {

    @Override
    public void draw(int x, int y, int width, int height, String color) {
        System.out.println(color + "linux");
    }

}

6) DirectXWindowImpl

public class DirectXWindow extends WindowImpl {
    @Override
    public void draw(int x, int y, int width, int height, String color) {
        System.out.println(color + "directX");

    }
}

results matching ""

    No results matching ""