Chapter 10: The Bridge Pattern Code Examples
TOC
TOC
TOC
Proper use of inheritance
Example 10-1
TOP
abstract public class Rectangle {
private double _x1,_y1,_x2,_y2;
public Rectangle (double x1,double y1,double x2,double y2) {
x1= x1;_y1= y1;_x2= x2;_y2= y2;
}
public void draw() {
drawLine(_x1,_y1,_x2,_y1);
drawLine(_x2,_y1,_x2,_y2);
drawLine(_x2,_y2,_x1,_y2);
drawLine(_x1,_y2,_x1,_y1);
}
abstract protected void drawLine(double x1,double y1,double x2,double y2);
}
Designing with inheritance
Example 10-2
TOP
abstract class Shape {
abstract public void draw();
}
// the only change to Rectangle is
abstract class Rectangle extends Shape {
//
// V1Rectangle and V2Rectangle don't change
abstract public class Circle extends Shape {
protected double _x,_y,_r;
public Circle (double x, double y, double r) {
_x= x;_y= y;_r= r;
}
public void draw() {
drawCircle();
}
abstract protected void drawCircle();
}
public class V1Circle extends Circle {
public V1Circle (double x,double y,double r) {
super(x,y,r);
}
protected void drawCircle () {
DP1.draw_a_circle(_x,_y,_r);
}
}
public class V2Circle extends Circle {
public V2Circle(double x,double y,double r) {
super(x,y,r);
}
protected void drawCircle () {
DP2.drawCircle(_x,_y,_r);
}
}
Relating this to the inheritance-based design
Example 10-3
TOP
public class Client {
static public void main () {
Shape myShapes[];
Factory myFactory= new Factory();
// get rectangles from some other source
myShapes= myFactory.getShapes();
for (int i= 0; i < myShapes.length; i++) {
myShapes[i].draw();
}
}
}
abstract public class Shape {
protected Drawing myDrawing;
abstract public void draw();
Shape (Drawing drawing) {
myDrawing= drawing;
}
protected void drawLine (double x1,double y1,double x2,double y2) {
myDrawing.drawLine(x1,y1,x2,y2);
}
protected void drawCircle (double x,double y,double r) {
myDrawing.drawCircle(x,y,r);
}
}
public class Rectangle extends Shape {
private double _x1,_y1,_x2,_y2;
public Rectangle (Drawing dp,double x1,double y1,double x2,double y2) {
super( dp);
x1= x1;_y1= y1;_x2= x2;_y2= y2;
}
public void draw() {
drawLine(_x1,_y1,_x2,_y1);
drawLine(_x2,_y1,_x2,_y2);
drawLine(_x2,_y2,_x1,_y2);
drawLine(_x1,_y2,_x1,_y1);
}
protected void drawLine(double x1,double y1,double x2,double y2) {
myDrawing.drawLine(x1,y1,x2,y2);
}
}
public class Circle extends Shape {
private double _x,_y,_r;
public Circle (Drawing dp,double x,double y,double r) {
super(dp);
x= x;_y= y;_r= r;
}
public void draw() {
myDrawing.drawCircle(_x,_y,_r);
}
}
abstract public class Drawing {
abstract public void drawLine(double x1,double y1,double x2,double y2);
abstract public void drawCircle(double x,double y,double r);
}
public class V1Drawing extends 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);
}
}
public class V2Drawing extends Drawing {
public void drawLine (double x1,double y1,double x2,double y2) {
// arguments are different in DP2
// and must be rearranged
DP2.drawLine(x1,x2,y1,y2);
}
public void drawCircle (double x, double y,double r) {
DP2.drawCircle(x,y,r);
}
}
|