The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently" . When a class varies often, the features of object-oriented programming become very useful because changes to a program's code can be made easily with minimal prior knowledge about the program. The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction. The bridge pattern can also be thought of as two layers of abstraction.
/** "Implementor" */
interface DrawingAPI {
public void drawCircle(double x, double y, double radius);
}
/** "ConcreteImplementor" 1*/
class DrawingAPI1 implements DrawingAPI {
public void drawCircle(double x, double y, double radius) {
System.out.println("API1.circle at "+ x +"," +y+","+ radius);
}
}
/** "Abstraction" */
class Shape {
private DrawingAPI drawingAPI;
public void draw();
}
/** "Refined Abstraction" */
class CircleShape implements Shape {
private double x, y, radius;
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() { this.drawingAPI.drawCircle(x, y, radius); }
}
/** "Client" */
class BridgePattern {
public static void main(String[] args) {
Shape[] shapes = new Shape[] {
new CircleShape(1, 2, 3, new DrawingAPI1()),
//new CircleShape(5, 7, 11, new DrawingAPIXXX()),
};
for (Shape shape : shapes) {
shape.draw();
}
}
}
Bridge Benefits
· Decouples an implementation so that it is not bound permanently to an interface.
· Abstraction and implementation can be extended independently.
· Changes to the concrete abstraction classes don’t affect the client.
Bridge Uses and Drawbacks
· Useful in graphic and windowing systems that need to run over multiple platforms.
· Useful any time you need to vary an interface and an implementation in different ways.
· Increases complexity.
Saturday, February 26, 2011
Subscribe to:
Post Comments (Atom)
Blog Archive
Total Pageviews
Popular Posts
-
Early men used improvised devices to tell their position on the globe. Following is the Latitude Hook of the Polynesians . It is used to...
-
When you first open visio and go to File ->New->Getting Started-> Software and Database ->Database Model Diagram-> (from Fil...
-
How did early man attempt to discover the circumference of the earth without ever traveling it? This is how it was accomplished by a man ...
-
We hope this and all knowledge is used for the benefit of humankind : to encourage us towards love and kindness and goodness. Men have at...
-
How would one measure the height of a mountain if one only had a quadrant (for measuring angle)? A Quadrant A quadrant is as illustrate...
-
The question of how to tell longitude accurately was contested for a very long time. People had figured out how to tell latitude by use o...
-
I looked online and (in two books) but did not find very good explanations of ternary search trees. Following is what I combined together fr...
-
This was an easy one to do. I volunteered to help some people at the office and they mistakenly thought I was a "DB2 expert" despi...
-
(see discussion ) Step 1 : install JDK 1.5+ and above Step 2: check JAVA_HOME is set Step 3: install the rjb gem gem install rjb -v 1.1....
No comments:
Post a Comment