Skip to content

Instantly share code, notes, and snippets.

@adam-singer
Forked from kencoba/Bridge.scala
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adam-singer/47d12b62b65aa4b7a70d to your computer and use it in GitHub Desktop.
Save adam-singer/47d12b62b65aa4b7a70d to your computer and use it in GitHub Desktop.
// 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