Created
April 4, 2014 16:17
-
-
Save azybler/9978002 to your computer and use it in GitHub Desktop.
Demonstrating Bridge Pattern in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** "Implementor" */ | |
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" */ | |
abstract class Shape { | |
protected DrawingAPI drawingAPI; | |
protected Shape(DrawingAPI drawingAPI){ | |
this.drawingAPI = drawingAPI; | |
} | |
public abstract void draw(); // low-level | |
public abstract void resizeByPercentage(double pct); // high-level | |
} | |
/** "Refined Abstraction" */ | |
class CircleShape extends Shape { | |
private double x, y, radius; | |
public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) { | |
super(drawingAPI); | |
this.x = x; this.y = y; this.radius = radius; | |
} | |
// 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 BridgePattern { | |
public static void main(String[] args) { | |
Shape[] shapes = new Shape[] { | |
new CircleShape(1, 2, 3, new DrawingAPI1()), | |
new CircleShape(5, 7, 11, new DrawingAPI2()), | |
}; | |
for (Shape shape : shapes) { | |
shape.resizeByPercentage(2.5); | |
shape.draw(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment