java桥接模式-软件设计师考试2013下半年 Posted on 2016-11-11 | Edited on 2019-11-03 | In 设计模式 | Comments: 类图及题目要求 代码可以运行12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091interface Drawing{ public void drawLine(double x1,double y1,double x2,double y2); public void drawCircle(double x,double y,double r);}class V1Drawing implements Drawing{ public void drawLine(double x1,double y1,double x2,double y2){DP1.draw_a_line(x1,y1,x2,y2);} public void drawCircle(double x,double y,double r){DP1.draw_a_circle(x, y, r);}}class V2Drawing implements Drawing{ public void drawLine(double x1,double x2,double y1,double y2){DP2.drawLine(x1,y1,x2,y2);} public void drawCircle(double x,double y,double r){DP2.drawcircle(x, y, r);}}class DP1{ static public void draw_a_line(double x1,double y1,double x2,double y2){ System.out.println("DP1 画的线"); } static public void draw_a_circle(double x,double y,double r){ System.out.println("DP1 画的圆"); }}class DP2{ static public void drawLine(double x1,double y1,double x2,double y2){ System.out.println("DP2 画的线"); } static public void drawcircle(double x,double y,double r){ System.out.println("DP2 画的圆"); }}abstract class Shape{ private Drawing _dp; public Shape(Drawing dp){ this._dp=dp; } abstract public void draw(); public void drawLine(double x1,double y1,double x2,double y2){ this._dp.drawLine(x1,x2,y1,y2); } public void drawCircle(double x,double y,double r){this._dp.drawCircle(x,y,r);}}class Rectangle extends Shape{ private double _x1,_x2,_y1,_y2; public Rectangle(Drawing dp,double x1,double x2,double y1,double y2){ super(dp); this._x1=x1; this._x2=x2; this._y1=y1; this._y2=y2; } public void draw(){ System.out.println("画长方形"+this._x1+this._x2+this._y1+this._y2); drawLine(_x1,_x2,_y1,_y2); }}class Circle extends Shape{ private double _x,_y,_r; public Circle (Drawing dp,double x,double y,double r){ super(dp); } public void draw(){drawCircle(_x,_y,_r);}}public class qiaojie { public static void main(String[] args){ V1Drawing v1=new V1Drawing(); V2Drawing v2=new V2Drawing(); Rectangle r1=new Rectangle(v1,1.0,2.0,3.0,4.0); r1.draw(); v2.drawCircle(2.1, 4.5, 5.6); v2.drawLine(2.4, 3.3 ,4.2, 5.1); }}