ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 구조패턴 : Bridge 패턴
    디자인패턴 2013. 1. 23. 10:00
    반응형

    분류 : 구조패턴(Structural Patterns)


    정의 :

    추상화를 구현에서 분리해서 서로 독립적으로 만드는 패턴.

    용도 :




    Abstraction : 추상 인터페이스를 정의. Implementor 참조를 관리

    RefinedAbstraction : Abstraction에서 정의된 인터페이스를 확장함.

    Implementor : 구현클래스를 위한 인터페이스를 정의함.

    ConcreteImplementor : Implementor 인터페이스를 구현함.



    소스

    
    // Implementor
    public interface DrawingAPI {
    	public void drawCircle(double x, double y, double radius);
    }
    
    
    //ConcreteImplementor 첫번째
    public class DrawingAPI1 implements DrawingAPI {
    
    	@Override
    	public void drawCircle(double x, double y, double radius) {
    		System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
    	}
    
    }
    
    
    //ConcreteImplementor 두번째
    public class DrawingAPI2 implements DrawingAPI {
    
    	@Override
    	public void drawCircle(double x, double y, double radius) {
    		System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
    	}
    
    }
    
    
    //Abstraction
    public abstract class Shape {
    	protected DrawingAPI drawingAPI;
    	 
       protected Shape(DrawingAPI drawingAPI){
          this.drawingAPI = drawingAPI;
       }
    	 
       public abstract void draw();                             // 저수준
       public abstract void resizeByPercentage(double pct);     // 고수준
    }
    
    
    //Refined Abstraction
    public 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;
       }
    	
    	//저수준, 즉 Implementation 명시
    	@Override
    	public void draw() {
    		// TODO Auto-generated method stub
    		drawingAPI.drawCircle(x, y, radius);
    	}
    
    	//고수준, Abstraction 명시
    	@Override
    	public void resizeByPercentage(double pct) {
    		// TODO Auto-generated method stub
    		radius *= pct;
    	}
    
    }
    
    //Client
    public 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();
    		}
       }
    
    }
    
    
    



    참고자료

    http://en.wikipedia.org/wiki/Bridge_pattern

    반응형

    '디자인패턴' 카테고리의 다른 글

    구조패턴 : Decorator 패턴  (0) 2013.01.28
    구조패턴 : Composite 패턴  (0) 2013.01.25
    구조패턴 : Adapter (Wrapper) 패턴  (0) 2013.01.21
    생성패턴 : Singleton 패턴  (0) 2013.01.16
    생성패턴 : Prototype 패턴  (0) 2013.01.15

    댓글

Designed by Tistory.