views:

337

answers:

2

Hi all,

I had spend some time on this Bridge pattern example from wikipedia, however, i still do not understand what is this bridge pattern trying to explain.

interface DrawingAPI {
    public void drawCircle(double x, double y, double radius);
}

/** "ConcreteImplementor" 1/2 */
class DrawingAPI1 implements DrawingAPI {
   public void drawCircle(double x, double y, double radius) {
        System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
   }
}

/** "ConcreteImplementor" 2/2 */
class DrawingAPI2 implements DrawingAPI {
   public void drawCircle(double x, double y, double radius) {
        System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
   }
}

/** "Abstraction" */
interface Shape {
   public void draw();                                            // low-level
   public void resizeByPercentage(double pct);     // high-level
}

/** "Refined Abstraction" */
class CircleShape implements Shape {
   private double x, y, radius;
   private DrawingAPI drawingAPI;
   public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {
       this.x = x;  this.y = y;  this.radius = radius;
       this.drawingAPI = drawingAPI;
   }

   // low-level i.e. Implementation specific
   public void draw() {
        drawingAPI.drawCircle(x, y, radius);
   }
   // high-level i.e. Abstraction specific
   public void resizeByPercentage(double pct) {
        radius *= pct;
   }
}

/** "Client" */
class Main {
   public static void main(String[] args) {
       Shape[] shapes = new Shape[2];
       shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1());
       shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2());

       for (Shape shape : shapes) {
           shape.resizeByPercentage(2.5);
           shape.draw();
       }
   }
}

The subclass CircleShape constructor takes 4 args, in its draw() method, the first 3 args are passed to the 4th arg which can be any subclass from DrawingAPI. So does this mean that using bridge pattern can increase flexibility? and are there more things this example can tell us?

Thanks!!!!

+4  A: 

A more concrete example why this is useful will make it clearer. Suppose DrawingAPI1 encapsulates your graphics driver, while DrawingAPI2 does the same thing for your printer driver. Then DrawingAPI is the generic API for your graphics system. It lets you draw a CircleShape to your monitor and print it on a piece of paper using the same code, you only have to pass in the different DrawingAPI implementations. However, if you pass DrawingAPI into Shape.draw() instead of passing it into the constructor it would be more flexible because then you can use the same object graph for the monitor and the printer.

FelixM
Thanks, i have a more clear view of pattern now.after google for hours about pattern, i had found a good link for patterns http://www.allappforum.com/java_design_patterns/creational_patterns.htm There is a simple example about bridge pattern.There is an interface called Switch, and two subclasses are called Bulb and Fan, each of the subclass has its own implementation for Switch.A nice and simple example.
A: 

I have no idea what the example tries to accomplish. It is the very simple and basic 'programming against interface' trick. Does it even deserve a name for a pattern?

Some people will look at the example and say, it is just an example of "Dependency Injection". Really? Do we really need fancy names for plain old generic basic tricks?

irreputable
If you have no idea what the answer is, then maybe you should not bother to post an answer, either.
Mathias
why people are intimidated by buzzwords and wondering if they missed anything, my type of answer is helpful.
irreputable