브릿지 패턴은 소프트웨어 공학에서 사용되는 디자인 패턴인데, 이는 "구현(implementation)으로부터 추상(abstraction) 레이어를 분리하여 이 둘이 서로 독립적으로 변화할 수 있도록 한다."
브릿지는 캡슐화(encapsulation), 집합(aggregation)을 사용하고 또한 다른 클래스들로 책임을 분리시키기 위해 상속(inheritance)를 사용할 수 있다.
어떤 클래스가 자주 바뀐다면(varies), 객체 지향 프로그래밍의 특징들은 아주 유용해질 수 있다. 왜냐하면 프로그램 코드를 수정하는데 프로그램에 대해 최소한만 알고도 쉽게 수정할 수 있기 때문이다. 브릿지 패턴은 어떤 클래스와 그 클래스가 하는 일이 자주 변화할 때 유용하게 사용될 수 있다. 여기에서 어떤 클래스는 구현(implementation)이라 생각할 수 있고, 그 클래스가 할 수 있는 일은 추상(abstraction)으로 생각할 수 있다. 브릿지 패턴은 추상화(abstraction)의 두 계층(layer)로 생각할 수도 있다.
브릿지 패턴은 종종 어댑터 패턴과 혼동될 수 있다. 사실, 브릿지 패턴은 종종 클래스 어댑터 패턴을 사용해서 구현되기도 한다. 아래에 나오는 자바 코드가 그 예가 될 것이다.
변종(Variant) : 추상(abstraction)이 사용되는 시점까지 구현의 존재를 미룸으로써 구현을 더욱 갈라놓을 수 있다.(정확히 이해는 안되지만 구현 객체를 최대한 늦게 생성함으로써 구현과 추상의 관계를 더욱 약하게 할 수 있다는 말인것 같네요;.
원문 : The implementation can be decoupled even more by deferring the presence of the implementation to the point where the abstraction is utilized.)
구조 Structure
Abstraction - 추상 인터페이스를 정의한다. Implementor에 대한 레퍼런스를 유지한다.
RefinedAbstraction - Abstraction에 의해 정의된 인터페이스를 확장한다.(extends)
Implementor - 구현 클래스를 위한 인터페이스를 정의한다.
ConcreteImplementor - Implementor 인터페이스를 구현한다.
예 Example
아래의 자바 프로그램은 'shape'를 이용한 예이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /** "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; } } /** "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(); } } }
|