Skip to content

Instantly share code, notes, and snippets.

@Arakaki
Created September 18, 2013 07:23
Show Gist options
  • Save Arakaki/6605669 to your computer and use it in GitHub Desktop.
Save Arakaki/6605669 to your computer and use it in GitHub Desktop.
command pattern in Scala
package com.mycode
object App {
def main(args : Array[String]) {
val receiver = new Receiver
val task = new ConcreteCommandTask(receiver)
val play = new ConcreteCommandTaskPlay(receiver)
val invoker = new Invoker
invoker.setCommand(task)
invoker.executeCommand
invoker.setCommand(play)
invoker.executeCommand
}
}
class Receiver {
def action1 {
println("掃除をする")
}
def action2 {
println("選択をする")
}
def action3 {
println("電話料金を支払う")
}
def action4 {
println("水道料金を支払う")
}
}
abstract class Command {
protected var receiver:Receiver
def execute{}
}
class ConcreteCommandTask(var receiver:Receiver) extends Command {
override def execute {
this.receiver.action1
this.receiver.action2
}
}
class ConcreteCommandTaskPlay(var receiver:Receiver) extends Command {
override def execute {
this.receiver.action3
this.receiver.action4
}
}
class Invoker {
private var command:Command = null
def getCommand = command
def setCommand(command:Command) {
this.command = command
}
def executeCommand {
this.command.execute
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment