노현진's Blog

Bridge Pattern

Bridge Pattern에 대해 설명하는 페이지입니다.

Posted
Preview Image
By HyunJinNo

Tags

Design Pattern, Java

1. Introduction

  • Purpose
    • Defines an abstraction object structure independently of the implementation object structure in order to limit coupling.
  • Use When
    • Abstractions and implementations should not be bound at compile time.
    • Abstractions and implementations should be independently extensible.
    • Implementation details should be hidden from the client.

2. Characteristics

  • Separate the variations in abstraction from the variations in implementation so that the number of classes only grows linearly.
  • Decouples an abstraction from its implementation so that the two can vary independently.

3. Participants

  • Abstraction
    • defines the abstraction's interface
    • maintains a reference to the implementor
    • forwards requests to the implementor (collaboration)
  • RefinedAbstraction
    • extends abstraction interface
  • Implementor
    • defines interface for implementations
  • ConcreteImplementor
    • implements Implementor interface, i.e. defines an implementation

4. How to Use (Example)

  • Abstraction

    java
    1public abstract class Shape {
    2    private Drawing dp;
    3
    4    public Shape(Drawing dp) {
    5        this.dp = dp;
    6    }
    7
    8    public abstract void draw();
    9
    10    public void drawLine(double x1, double y1, double x2, double y2) {
    11        this.dp.drawLine(x1, y1, x2, y2);
    12    }
    13
    14    public void drawCircle(double x, double y, double r) {
    15        this.dp.drawCircle(x, y, r);
    16    }
    17}
  • RefinedAbstraction

    java
    1public class Rectangle extends Shape {
    2    private double x1;
    3    private double y1;
    4    private double x2;
    5    private double y2;
    6
    7    public Rectangle(Drawing dp, double x1, double y1, double x2, double y2) {
    8        super(dp);
    9        this.x1 = x1;
    10        this.y1 = y1;
    11        this.x2 = x2;
    12        this.y2 = y2;
    13    }
    14
    15    @Override
    16    public void draw() {
    17        drawLine(x1, y1, x2, y1);
    18        drawLine(x2, y1, x2, y2);
    19        drawLine(x2, y2, x1, y2);
    20        drawLine(x1, y2, x1, y1);
    21    }
    22}
    java
    1public class Circle extends Shape {
    2    private double x;
    3    private double y;
    4    private double r;
    5
    6    public Circle(Drawing dp, double x, double y, double r) {
    7        super(dp);
    8        this.x = x;
    9        this.y = y;
    10        this.z = z;
    11    }
    12
    13    public void draw() {
    14        drawCircle(x, y, r);
    15    }
    16}
  • Implementor

    java
    1public abstract class Drawing {
    2    public abstract void drawLine(double x1, double y1, double x2, double y2);
    3    public abstract void drawCircle(double x, double y, double r);
    4}
  • ConcreteImplementor

    java
    1public class V1Drawing extends Drawing {
    2    public void drawLine(double x1, double y1, double x2, double y2) {
    3        DP1.draw_a_line(x1, y1, x2, y2);
    4    }
    5
    6    public void drawCircle(double x, double y, double r) {
    7        DP1.draw_a_circle(x, y, r);
    8    }
    9}
    java
    1public class V2Drawing extends Drawing {
    2    public void drawLine(double x1, double y1, double x2, double y2) {
    3        DP2.drawLine(x1, y1, x2, y2);
    4    }
    5
    6    public void drawCircle(double x, double y, double r) {
    7        DP2.drawCircle(x, y, r);
    8    }
    9}

© HyunJinNo. Some rights reserved.