Bridge

Bridge ํŒจํ„ด์€ ๊ธฐ๋Šฅ(Abstraction)๊ณผ ๊ตฌํ˜„(Implementation)์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์„œ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

       +------------------+
       |  Abstraction     |  <-- ๊ธฐ๋Šฅ ๊ณ„์ธต (์ถ”์ƒ ํด๋ž˜์Šค)
       +------------------+
                |
                |  (๊ตฌํ˜„ ๊ฐ์ฒด ์ฐธ์กฐ)
                v
       +------------------+
       | RefinedAbstraction|
       +------------------+
                |
                |  (๊ตฌํ˜„ ๊ณ„์ธต๊ณผ ์—ฐ๊ฒฐ)
                v
       +------------------+
       |  Implementor     |  <-- ๊ตฌํ˜„ ๊ณ„์ธต (์ธํ„ฐํŽ˜์ด์Šค)
       +------------------+
                /   \
               /     \
              v       v
+------------------+  +---------------------+
|ConcreteImplementor1| |ConcreteImplementor2|
+------------------+  +---------------------+

์ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์†์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  ๋‘ ๊ณ„์ธต์„ ๋…๋ฆฝ์ ์œผ๋กœ ๋ฐœ์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ™•์žฅ์„ฑ๊ณผ ์œ ์—ฐ์„ฑ์ด ํฌ๊ฒŒ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.


How do code

// ๊ตฌํ˜„๋ถ€ ์ธํ„ฐํŽ˜์ด์Šค (Implementor)
@FunctionalInterface
public interface DrawingAPI {
    void drawCircle(double x, double y, double radius);
}

public class BridgePattern {
    // ๊ธฐ๋Šฅ ๊ณ„์ธต (Abstraction)
    public static abstract class Shape {
        protected DrawingAPI drawingAPI;

        protected Shape(DrawingAPI drawingAPI) {
            this.drawingAPI = drawingAPI;
        }

        public abstract void draw();
        public abstract void resizeByPercentage(double pct);
    }

    // ๊ตฌ์ฒด์ ์ธ ๊ธฐ๋Šฅ ํด๋ž˜์Šค
    public static 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;
        }

        @Override
        public void draw() {
            drawingAPI.drawCircle(x, y, radius);
        }

        @Override
        public void resizeByPercentage(double pct) {
            radius *= (1.0 + pct / 100.0);
        }
    }

    public static void main(String[] args) {
        // ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ DrawingAPI ๊ตฌํ˜„
        DrawingAPI api1 = (x, y, radius) ->
            System.out.println("Lambda API1 - ์›: (" + x + ", " + y + ") ๋ฐ˜์ง€๋ฆ„: " + radius);

        DrawingAPI api2 = (x, y, radius) ->
            System.out.println("Lambda API2 - ์›: (" + x + ", " + y + ") ๋ฐ˜์ง€๋ฆ„: " + radius);

        Shape circle1 = new CircleShape(1, 2, 3, api1);
        Shape circle2 = new CircleShape(5, 7, 11, api2);

        circle1.resizeByPercentage(50);
        circle2.resizeByPercentage(50);

        circle1.draw();
        circle2.draw();
    }
}

Last updated