Created
January 31, 2016 21:26
-
-
Save shkesar/9f3952729ddb92e21e39 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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