了解的不是很深 。用到在看。
package com.test.DPs.JieGou.Bridge;
/**
* 結構型:Bridge-橋接 橋接:作用面為 對象
*
* 用途:将抽象部分與它的實作部分分離,使它們都可以獨立的變化。
*/
interface DrawAPI{
public void drawCircle(int radius, int x, int y);
}
class RedCircle implements DrawAPI{
@Override
public void drawCircle(int radius, int x, int y){
System.out.println("Drawing Circle[ color: red, radius:"+radius
+ ", x:"+x+", y:"+y+"]");
}
}
class GreenCircle implements DrawAPI{
@Override
public void drawCircle(int radius, int x, int y){
System.out.println("Drawing Circle[ color: green, radius:"+radius
+ ", x:"+x+", y:"+y+"]");
}
}
abstract class Shape{
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
public abstract void draw();
}
/**
* Circle類将DrawAPI和Shape類進行了橋接。
* Shepe類給出了參數,DrawAPI依據參數進行了實作。
* 了解:橋接兩邊分别是:抽象和重載
*/
class Circle extends Shape{
private int x, y, radius;
public Circle(int x, int y, int radius, DrawAPI drawAPI){
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw(){
drawAPI.drawCircle(radius, x, y);
}
}
//用戶端使用代碼
/*
Shape redCircle = new Circle(100,100, 10, new RedCircle());
Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
redCircle.draw();
greenCircle.draw()
*/