'''브리지 패턴'''(Bridge pattern)이란 구현부에서 추상층을 분리하여 각자 독립적으로 변형할 있게 하는 패턴이다. ==구조== 구조 == 코드 == === 자바 === /** "Implementor" */ interface DrawingAPI public void x, double y, double radius); /** 1/2 */ class DrawingAPI1 implements DrawingAPI public void x, double y, double radius) at %f:%f radius %f\n", x, y, radius); /** 2/2 */ class DrawingAPI2 implements DrawingAPI public void x, double y, double radius) at %f:%f radius %f\n", x, y, radius); /** "Abstraction" */ interface Shape public void draw(); // low-level public void pct); // high-level /** "Refined Abstraction" */ class CircleShape implements Shape private double x, y, radius; private DrawingAPI drawingAPI; public x, double y, double radius, DrawingAPI drawingAPI) this.x x; this.y y; this.radius radius; this.drawingAPI drawingAPI; // low-level i.e. Implementation specific public void draw() y, radius); // high-level i.e. Abstraction specific public void pct) radius *= pct; /** "Client" */ class BridgePattern public static void main(String args) Shape shapes new Shape2; shapes0 new CircleShape(1, 2, 3, new DrawingAPI1()); shapes1 new CircleShape(5, 7, 11, new DrawingAPI2()); for (Shape shape shapes) shape.draw(); === C#=== using System; /** "Implementor" */ interface IDrawingAPI void x, double y, double radius); /** 1/2 */ class DrawingAPI1 IDrawingAPI public void x, double y, double radius) at {0}:{1} radius {2}", x, y, radius); /** 2/2 */ class DrawingAPI2 IDrawingAPI public void x, double y, double radius) at {0}:{1} radius {2}", x, y, radius); /** "Abstraction" */ interface IShape void Draw(); // low-level (i.e. void pct); // high-level (i.e. /** "Refined Abstraction" */ class CircleShape IShape private double x, y, radius; private IDrawingAPI drawingAPI; public x, double y, double radius, IDrawingAPI drawingAPI) this.x x; this.y y; this.radius radius; this.drawingAPI drawingAPI; // low-level (i.e. public void Draw() y, radius); // high-level (i.e. public void pct) radius *= pct; /** "Client" */ class BridgePattern public static void Main(string args) IShape shapes new IShape2; shapes0 new CircleShape(1, 2, 3, new DrawingAPI1()); shapes1 new CircleShape(5, 7, 11, new DrawingAPI2()); foreach (IShape shape in shapes) shape.Draw(); ===C++=== '''출력 결과''' API1.circle at 1:2 7.5 API2.circle at 5:7 27.5 #include using namespace std; /* Implementor*/ class DrawingAPI public: virtual void x, double y, double radius) 0; virtual }; /* Concrete ImplementorA*/ class DrawingAPI1 public DrawingAPI public: void x, double y, double radius) cout void pct) m_radius *= pct; private: double DrawingAPI *m_drawingAPI; }; int main(void) DrawingAPI1 dap1; DrawingAPI2 dap2; CircleShape CircleShape circle1.draw(); circle2.draw(); return 0; === GO === package main import "fmt" // Implementor type DrawingAPI interface float32, float32) // 1/2 type DrawingAPI1 struct{} func (d *DrawingAPI1) drawCircle(x float32, float32, radius float32) at %f:%f radius %f\n", x, y, radius) // 2/2 type DrawingAPI2 struct{} func (d *DrawingAPI2) drawCircle(x float32, float32, radius float32) at %f:%f radius %f\n", x, y, radius) // Abstraction type Shape interface draw() // Refined Abstraction type CircleShape struct x, y, radius float32 drawingAPI DrawingAPI // Implementation specific func (c *CircleShape) draw() c.y, c.radius) // Abstraction specific func (c *CircleShape) float32) c.radius *= pct // Client func main() shape1 := &CircleShape{1, 2, 3, &DrawingAPI1{}} shape1.draw() shape2 := &CircleShape{5, 7, 11, &DrawingAPI2{}} shape2.draw() === Python3 === Implementor class DrawingAPI: def x, y, radius): pass 1/2 class def x, y, radius): at {x}:{y} {radius}") 2/2 class def x, y, radius): at {x}:{y} {radius}") Abstraction class Shape: def draw(self): pass def pass Refined Abstraction class radius drawing_api None def __init__(self, x, y, radius, drawing_api): self.x self.y self.radius radius drawing_api def draw(self): self.y, self.radius) def pct): self.radius *= pct def main(): dap1 DrawingAPI1() dap2 DrawingAPI2() circle1 CircleShape(1, 2, 3, dap1) circle2 CircleShape(5, 7, 11, dap2) circle1.draw() circle2.draw() if __name__ == "__main__": main() 분류:소프트웨어 디자인 패턴