-
-
Save adam-singer/47d12b62b65aa4b7a70d to your computer and use it in GitHub Desktop.
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
// http://en.wikipedia.org/wiki/Bridge_pattern | |
trait DrawingAPI { | |
def drawCircle(x:Double, y: Double, radius:Double) | |
} | |
class DrawingAPI1 extends DrawingAPI { | |
override def drawCircle(x: Double, y: Double, radius: Double) = { | |
printf("API1.circle at %f:%f radius %f\n", x, y, radius) | |
} | |
} | |
class DrawingAPI2 extends DrawingAPI { | |
override def drawCircle(x: Double, y: Double, radius: Double) = { | |
printf("API2.circle at %f:%f radius %f\n", x, y, radius) | |
} | |
} | |
abstract class Shape(drawingAPI: DrawingAPI) { | |
def draw | |
def resizeByPercentage(pct: Double) | |
} | |
class CircleShape(x: Double, y: Double, radius: Double, drawingAPI: DrawingAPI) | |
extends Shape(drawingAPI) { | |
var _x: Double = x | |
var _y: Double = y | |
var _radius: Double = radius | |
override def draw = drawingAPI.drawCircle(_x, _y, _radius) | |
override def resizeByPercentage(pct: Double) = _radius *= pct | |
} | |
object BridgeSample { | |
def main(args: Array[String]) = { | |
var shapes = List( | |
new CircleShape(1,2,3, new DrawingAPI1()), | |
new CircleShape(5,7,11, new DrawingAPI2()) | |
) | |
shapes.foreach ((s:Shape) => { | |
s.resizeByPercentage(2.5) | |
s.draw | |
}) | |
} | |
} | |
BridgeSample.main(Array()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment