形状的抽象类
/** * @author wxy * @description 形状的抽象类 * @data 2020/7/2 */ public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI){ this.drawAPI = drawAPI; } public abstract void draw(); }圆的接口
/** * @description 圆的接口 * @author wxy */ public interface DrawAPI { public void drawCircle(int radius, int x, int y); }接口的实现
public 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 +"]"); } }接口的实现
public 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 +"]"); } }抽象类的子类,实现桥接
public class Circle extends Shape{ private int x, y, radius; protected Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius; } @Override public void draw() { drawAPI.drawCircle(radius,x,y); } }桥接模式
/** * @author wxy * @description 桥接模式 * @data 2020/7/2 */ public class BridgePatternDemo { public static void main(String[] args) { Shape redCircle = new Circle(100,100, 10, new RedCirCle()); Shape greenCircle = new Circle(100,100, 10, new GreenCirCle()); redCircle.draw(); greenCircle.draw(); } }