Created
September 18, 2013 07:23
-
-
Save Arakaki/6605669 to your computer and use it in GitHub Desktop.
command pattern in Scala
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
| 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