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
  • Select an option

  • Save adam-singer/47d12b62b65aa4b7a70d to your computer and use it in GitHub Desktop.

Select an option

Save adam-singer/47d12b62b65aa4b7a70d to your computer and use it in GitHub Desktop.

Revisions

  1. @kencoba kencoba created this gist Feb 21, 2012.
    48 changes: 48 additions & 0 deletions Bridge.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    // 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())