您的位置:首页 > 其它

scala实现解释器模式

2015-08-31 20:20 357 查看
本来是看解释器模式,但感觉scala的实现的流利接口可以更优雅和简单的实现,所以并没有选择用解释器模式,而是采用流利接口实现的

package com.linewell.modeldesgin.interpreter

/**
* 行为特质
* Created by ctao on 2015/8/31.
*/
trait Action

/**
* 方向特质
*/
trait Direction

/**
* 向上实现方向特质
*/
object Up extends Direction

/**
* 向下实现方向特质
*/
object Down extends Direction

/**
* 向左实现方向特质
*/
object Left extends Direction

/**
* 向右实现方向特质
*/
object Right extends Direction

/**
* run实现行为特质
*/
object Run extends Action

/**
* move实现行为特质
*/
object Move extends Action

/**
* then实现行为特质
*/
object Then extends Action


/**
* robot up move length 1 and right run length 3
* @param x x坐标
* @param y y坐标
*/
class Robot(var x: Int = 0, var y: Int = 0) {
/**
* 行为
*/
private var actions: String = null
/**
* 方向
*/
private var directions: Direction = null
/**
* 记录方向
*/
private var tag: String = null

/**
* 向上
* @param action 行为
* @return 自身类型
*/
def up(action: Action): this.type = {
action match {
case Run => actions = "快速移动"
case Move => actions = "移动"
}
directions = Up
this
}


/**
* 向下
* @param action 行为
* @return 自身类型
*/
def down(action: Action): this.type = {
action match {
case Run => actions = "快速移动"
case Move => actions = "移动"
}
directions = Down
this
}

/**
* 向左
* @param action 行为
* @return 自身类型
*/
def left(action: Action): this.type = {
action match {
case Run => actions = "快速移动"
case Move => actions = "移动"
}
directions = Left
this
}

/**
* 向右
* @param action 行为
* @return 自身类型
*/
def right(action: Action): this.type = {
action match {
case Run => actions = "快速移动"
case Move => actions = "移动"
}
directions = Right
this
}

/**
* 长度设置
* @param length 长度
* @return 自身类型
* @return 自身类型
*/
def length(length: Int): this.type = {
directions match {
case Up => y += length; tag = "向上"
case Down => y -= length; tag = "向下"
case Left => x -= length; tag = "向左"
case Right => x += length; tag = "向右"
}
println(s"机器人$tag$actions :$length ,此时的位置为x坐标:$x,y坐标:$y")
this
}

/**
* and方法用于过度
* @param word Then
* @return 自身类型
*/
def and(word: Then.type) = this


}


package com.linewell.modeldesgin.interpreter

/**
* 测试客户端
* Created by ctao on 2015/8/31.
*/
object ClientRobot extends App {
val robot = new Robot
robot up Run length 10 and Then left Move length 5 and Then right Run length 3 and Then down Move length 8
}


package com.linewell.modeldesgin.interpreter

/**
* 展示
*/
object Show

/**
* 然后
*/
object ThenBug

/**
* 回头
*/
object Around

/**
* bug类
* @param pos 坐标
* @param dir 初始方向
*/
class Bug(var pos: Int = 0, var dir: Int = 1) {
def move(length: Int) = {
pos += length * dir
println(s"移动$length")
this
}

def turn(word: Around.type = null) = {
dir = -dir
println("回头")
this
}

def show() = {
println(s"虫子的位置:$pos")
this
}


def and(word: ThenBug.type) = this

def and(word: Show.type) = this.show()


}


package com.linewell.modeldesgin.interpreter

/**
* 测试客户端,流利接口,类似解释器模式
* Created by ctao on 2015/8/31.
*/
object ClientBug extends App {
val robot = new Bug(1)
robot move 4 and Show and ThenBug move 6 and Show turn Around move 5 and Show
}


package com.linewell.modeldesgin.interpreter

/**
* 参数
* Created by ctao on 2015/8/31.
*/
trait Prop

/**
* 标题
*/
object Title extends Prop

/**
* 作者
*/
object Author extends Prop

class Book(var author: String = "", var title: String = "") {
private var useArgs: Any = null

def set(obj: Prop): this.type = {
useArgs = obj; this
}


def to(args: String): this.type = useArgs match {
case Title => title = args; this
case Author => author = args; this
}
}
package com.linewell.modeldesgin.interpreter

/**
* 测试客户端
* Created by ctao on 2015/8/31.
*/
object ClientBook extends App {
val book = new Book
book set Author to "chen" set Title to "scala"
println("author:"+book.author+" and title:"+book.title)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: