브리지 패턴

1 개요[ | ]

bridge pattern
브리지 패턴, 가교 패턴
  • 기능 계층과 구현 계층이 분리된 구조
  • 구현부에서 추상층을 분리하여 각자 독립적으로 변형할 수 있게 하는 패턴

Bridge UML class diagram.svg

W3sDesign Bridge Design Pattern UML.jpg

2 예제 (Java)[ | ]

/** "Client" */
class BridgePattern {
   public static void main(String[] args) {
       Shape[] shapes = new Shape[] {
           new CircleShape(1, 2, 3, new DrawingAPI1()),
           new CircleShape(5, 7, 11, new DrawingAPI2()),
       };
 
       for (Shape shape : shapes) {
           shape.resizeByPercentage(2.5);
           shape.draw();
       }
   }
}

/** "Implementor" */
interface DrawingAPI {
    public void drawCircle(double x, double y, double radius);
}
 
/** "ConcreteImplementor"  1/2 */
class DrawingAPI1 implements DrawingAPI {
   public void drawCircle(double x, double y, double radius) {
        System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
   }
}
 
/** "ConcreteImplementor" 2/2 */
class DrawingAPI2 implements DrawingAPI {
   public void drawCircle(double x, double y, double radius) {
        System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
   }
}
 
/** "Abstraction" */
abstract class Shape {
   protected DrawingAPI drawingAPI;
 
   protected Shape(DrawingAPI drawingAPI){
      this.drawingAPI = drawingAPI;
   }
 
   public abstract void draw();                             // low-level
   public abstract void resizeByPercentage(double pct);     // high-level
}
 
/** "Refined Abstraction" */
class CircleShape extends Shape {
   private double x, y, radius;
   public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {
      super(drawingAPI);
      this.x = x;  this.y = y;  this.radius = radius;
   }
 
   // low-level i.e. Implementation specific
   public void draw() {
        drawingAPI.drawCircle(x, y, radius);
   }
   // high-level i.e. Abstraction specific
   public void resizeByPercentage(double pct) {
        radius *= pct;
   }
}

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}