public class TestBridge {
public static void main(String[] args) {
Shape redCircle=new Circle(new RedPen(),10);
Shape rectangle=new Rectangle(new GreenPen(),10,9);
redCircle.draw();
rectangle.draw();
}
}
public interface DrawAPI {
public void draw(int redius,int x,int y);
}
public abstract class Shape {
protected DrawAPI drawAPI;
public Shape(DrawAPI drawAPI) {
this.drawAPI = drawAPI;
}
public abstract void draw();
}
public class Rectangle extends Shape {
private int x;
private int y;
public Rectangle(DrawAPI drawAPI,int x,int y) {
super(drawAPI);
this.x=x;
this.y=y;
}
@Override
public void draw() {
drawAPI.draw(0,x,y);
}
}
public class Circle extends Shape {
private int redius;
public Circle(DrawAPI drawAPI, int redius) {
super(drawAPI);
this.redius = redius;
}
@Override
public void draw() {
drawAPI.draw(redius,0,0);
}
}
public class GreenPen implements DrawAPI {
@Override
public void draw(int redius, int x, int y) {
System.out.println("用绿色笔画图,redius"+redius);
}
}
public class RedPen implements DrawAPI{
@Override
public void draw(int redius, int x, int y) {
System.out.println("用红色笔画图,redius"+redius);
}
}