- 浏览: 13195 次
- 性别:
- 来自: 南京
文章分类
最新评论
本来是看解释器模式,但感觉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)
}
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)
}
发表评论
-
linux mongodb3安装
2015-10-29 15:33 259http://blog.csdn.net/cjuexuan/a ... -
scala代码重构(用递归代替var和while)
2015-09-07 19:47 380原有代码,其中使用了while循环和var /** ... -
再探设计模式之观察者模式(股票通知系统)
2015-09-05 10:28 496package com.linewell.modeldesig ... -
再探设计模式之状态模式(纸牌游戏的开发)
2015-09-04 13:52 537状态模式主要用于检测和切换状态,在状态模式下状态的转化对于客户 ... -
再探设计模式之访问者模式
2015-09-03 14:44 478访问者模式的使用条件比较苛刻主要可以以用于新增访问者时只需要 ... -
scala实现访问者模式
2015-09-02 20:54 374package com.linewell.modeldesgi ... -
scala实现模版方法模式
2015-09-02 20:54 415package com.linewell.modeldesgi ... -
scala实现状态模式
2015-09-02 16:37 385package com.linewell.modeldesgi ... -
scala实现观察者模式
2015-09-02 16:35 588package com.linewell.modeldesgi ... -
scala实现迭代器模式
2015-09-01 22:56 465package com.linewell.modeldesgi ... -
scala实现备忘录模式
2015-09-01 22:54 339package com.linewell.modeldesgi ... -
scala实现中介者模式
2015-09-01 14:48 286package com.linewell.modeldesgi ... -
scala实现命令模式
2015-08-30 21:20 269package com.linewell.modeldesgi ... -
scala实现职责链模式
2015-08-30 13:38 304package com.linewell.modeldesgi ... -
scala实现享元模式
2015-08-30 00:17 335package com.linewell.modeldesgi ... -
scala实现代理模式
2015-08-30 00:16 486package com.linewell.modeldesgi ... -
scala实现外观模式
2015-08-28 21:41 310package com.linewell.modeldesgi ... -
scala实现装饰者模式
2015-08-16 19:52 394package com.linewell.modeldesgi ... -
scala实现组合模式
2015-08-16 11:14 833package com.linewell.modeldesgi ... -
scala实现桥接模式
2015-08-16 11:13 311package com.linewell.modeldesgi ...
相关推荐
scala:Scala和设计模式 ...在当前软件Software设计中最流行要算GoF这本书...用方便性也很重要不妨让我们来看看Scala(种类似Ruby/Lisp的类语言它定程度上降低了模式实现繁琐)中常用 些模式 ; ; ; ; 1.Singleton(单体模式) ;
响应式架构 消息模式Actor实现与Scala.Akka应用集成 响应式架构 消息模式Actor实现与Scala.Akka应用集成
scala连接redis哨兵模式 demo 使用scala的redis库(csdn)————程序
自己用scala实现的朴素贝叶斯算法。最近学习machine learning,由于之前学习了spark,于是使用scala尝试写写代码
scala 设计模式,详细讲述scala设计模式
差分进化算法的Scala实现_Scala_代码_下载
scala正则表达式在模式匹配中的应用:提取模式的分组值,通过代码理解正则表达式与模式匹配的原理。
消息模式Actor实现与Scala、Akka应用集成
Scala设计模式,Scala高阶参考书。
scala for循环中的模式匹配:1)变量模式匹配2)常量模式匹配3)变量绑定模式匹配4)类型模式匹配5)构造函数模式匹配6)序列模式匹配
[Manning Publications] Play 框架开发 (Scala 实现) (英文版) [出版日期] 2013年10月11日[图书页数] 328页 [图书语言] 英语[图书格式] PDF 格式
scala语言和python一样都是交互式的语言,操作简单。这是wordcount的scala实现,简单明了,比java实现简单很多,希望对大家有所帮助
不到一百行的代码教你在spark平台中使用scala实现kmeans算法。简单易懂,大量注释。适合初学者参考理解。本程序在intelliJ IDEA2016.1.1 中编程,运行在spark1.6.1 scala2.10.4本地模式下运行成功。 数据集:(其实...
响应式架构++消息模式Actor实现与Scala.Akka应用集成+,沃恩·弗农+
响应式架构 消息模式Actor实现与Scala.Akka应用集成 高清扫描版
用500行scala代码实现SQL解析器的英文版论文,看scala强悍的算法设计。
基于spark-ml,scala实现gbdt 算法,附带libsvm训练集和普通训练集实现,GBDT (Gradient Boosting Decision Tree) 梯度提升迭代决策树。GBDT 也是 Boosting 算法的一种,但是和 AdaBoost 算法不同(AdaBoost 算法...
Scala语法简明教程.pdf