Skip to content

Instantly share code, notes, and snippets.

@shkesar
Created January 31, 2016 21:26
Show Gist options
  • Save shkesar/9f3952729ddb92e21e39 to your computer and use it in GitHub Desktop.
Save shkesar/9f3952729ddb92e21e39 to your computer and use it in GitHub Desktop.
trait Component {
val childrens = new collection.mutable.ArrayBuffer[Component]()
//val parent: Component
def operation: Unit
def add(component: Component) = ()
def remove(component: Component) = ()
def getChildrens: Iterator[Component] = collection.Iterator.empty
}
abstract class Leaf extends Component
abstract class Composite extends Component {
override def add(component: Component) = { childrens += component }
override def remove(component: Component) = { childrens -= component }
override def getChildrens = childrens.toIterator
}
class Leaf1 extends Leaf {
def operation = {
println("Leaf1")
}
}
class Composite1 extends Composite {
def operation = getChildrens.foreach(_.operation)
}
object Main extends App {
val c = new Composite1()
c.add(new Leaf1())
c.add(new Leaf1())
c.operation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment