`
fineqtbull
  • 浏览: 50785 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

Scala学习笔记(Scala编程第10章 Composition and Inheritance 例子)

阅读更多
《Programming In Scala》第10章 Composition and Inheritance 例子。

源代码:
/**
 * 《Programming In Scala》第10章 Composition and Inheritance 例子
 */
package layout

//工厂单例对象
object Element {
    //数组元素,内容为多行
    private class ArrayElement(
        val contents: Array[String]
    ) extends Element
    //行元素,内容为单行
    private class LineElement(s: String) extends Element {
        val contents = Array(s)
        override def width = s.length
        override def height = 1
    }
    //单一字符重复填充元素,填充成指定长和宽的元素
    private class UniformElement(
        ch: Char,
        override val width: Int,
        override val height: Int
    ) extends Element {
        private val line = ch.toString * width
        def contents = Array.make(height, line)
    }
    //工厂方法,创建多行元素
    def elem(contents: Array[String]): Element =
        new ArrayElement(contents)
    //工厂方法,创建单字填充元素
    def elem(chr: Char, width: Int, height: Int): Element =
        new UniformElement(chr, width, height)
    //工厂方法,创建单行元素
    def elem(line: String): Element =
        new LineElement(line)
}

//引入所有工厂方法
import Element.elem

//图形元素基类
abstract class Element {
    //元素内容
    def contents: Array[String]
    //元素宽度(字符数)
    def width: Int = contents(0).length
    //元素高度(字符数)
    def height: Int = contents.length
    //将自己放在that上面
    def above(that: Element): Element = {
        val this1 = this widen that.width
        val that1 = that widen this.width
        elem(this1.contents ++ that1.contents)
    }
    //将自己放在that的左边
    def beside(that: Element): Element = {
        val this1 = this heighten that.height
        val that1 = that heighten this.height
        elem(
            for ((line1, line2) <- this1.contents zip that1.contents)
            yield line1 + line2)
    }
    //扩充宽度,两边用空格填充
    def widen(w: Int): Element =
        if (w <= width) this
        else {
            val left = elem(' ', (w - width) / 2, height)
            var right = elem(' ', w - width - left.width, height)
            left beside this beside right
        }
    //扩充高度,上下用空格填充
    def heighten(h: Int): Element =
        if (h <= height) this
        else {
            val top = elem(' ', width, (h - height) / 2)
            var bot = elem(' ', width, h - height - top.height)
            top above this above bot
        }

    //返回该元素的文本描述。
    //内容数组中的每个元素都是一行。
    override def toString = contents mkString "\n"
}

//主单例对象,包括文件主函数
object Spiral {
    //空格元素
    val space = elem(" ")
    //转角元素
    val corner = elem("+")
    //构建螺旋图形,nEdges为构成螺旋的边数,direction为螺旋最后一条边的方向
    def spiral(nEdges: Int, direction: Int): Element = {
        //螺旋最内部元素
        if (nEdges == 1)
            elem("+")
        else {
            //递归调用,边数逐次减一
            val sp = spiral(nEdges - 1, (direction + 3) % 4)
            //竖行
            def verticalBar = elem('|', 1, sp.height)
            //横行
            def horizontalBar = elem('-', sp.width, 1)
            //上
            if (direction == 0)
                (corner beside horizontalBar) above (sp beside space)
            //右
            else if (direction == 1)
                (sp above space) beside (corner above verticalBar)
            //下
            else if (direction == 2)
                (space beside sp) above (horizontalBar beside corner)
            //左
            else
                (verticalBar above corner) beside (space above sp)
        }
    }

    def main(args: Array[String]) {
        //打印n边螺旋
        val nSides = 6
        println(spiral(nSides, 0))
    }
}



执行结果:

+-----
|     
| +-+ 
| + | 
|   | 
+---+ 
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics