Commandパターン。
あまりScalaっぽくない。
trait Command {
def execute: Unit
}class CommandA extends Command {
def execute = println("Execute CommandA")
}class CommandB extends Command {
def execute = println("Execute CommandB")
}class Receiver {
var commands = List[Command]()
def addCommand(command: Command) = { commands :+= command }
def execute = commands.foreach(_.execute)
}object Main {
def main(args: Array[String]) = {
val receiver = new Receiver
receiver.addCommand(new CommandA)
receiver.addCommand(new CommandA)
receiver.addCommand(new CommandB)
receiver.execute
}
}
Listへの末尾追加が『:+=』なのはちょっとキモイ。