Created
February 21, 2012 11:08
-
-
Save kencoba/1875890 to your computer and use it in GitHub Desktop.
Composite pattern (Design Patterns in Scala)
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/Composite_pattern | |
trait Graphic { | |
def print | |
} | |
class CompositeGraphic extends Graphic { | |
private var mChildGraphics = List[Graphic]() | |
override def print = { | |
println("(") | |
mChildGraphics.foreach ((g:Graphic) => { | |
g.print | |
}) | |
println(")") | |
} | |
def add(graphic: Graphic) = { | |
mChildGraphics = graphic :: mChildGraphics | |
} | |
def remove(graphic: Graphic) = { | |
mChildGraphics = mChildGraphics.filterNot(_ == graphic) | |
} | |
} | |
class Ellipse extends Graphic { | |
override def print = { | |
println("Ellipse") | |
} | |
} | |
object CompositeSample { | |
def main(args: Array[String]) = { | |
val ellipse1 = new Ellipse | |
val ellipse2 = new Ellipse | |
val ellipse3 = new Ellipse | |
val ellipse4 = new Ellipse | |
var graphic = new CompositeGraphic | |
var graphic1 = new CompositeGraphic | |
var graphic2 = new CompositeGraphic | |
graphic1.add(ellipse1) | |
graphic1.add(ellipse2) | |
graphic1.add(ellipse3) | |
graphic2.add(ellipse4) | |
graphic.add(graphic1) | |
graphic.add(graphic2) | |
graphic.print | |
} | |
} | |
CompositeSample.main(Array()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment