Decorator:kotlin

気を付けたこと

  • kotlinでは一つのファイルに複数クラスを書けるので一つのファイルにまとめた
  • コンストラクタでフィールドの宣言と初期化が出来る
  • forの書き方が少し違う
fun main() {
    val b1 = StringDisplay("Hello,world")
    val b2 = SideBorder(b1, '#')
    val b3 = FullBorder(b2)
    b1.show()
    b2.show()
    b3.show()
    val b4 =
        SideBorder(
            FullBorder(
                SideBorder(
                    FullBorder(
                        StringDisplay("こんにいちは")
                    ), '*'
                )
            ),
            '/'
        )
    b4.show()


}
abstract class Display{
    abstract fun getColumns():Int
    abstract fun getRows():Int
    abstract fun getRowText(row: Int): String?
    fun show(){
        for (i in 0 until getRows()) println(getRowText(i))
    }
}
class StringDisplay(private val string: String):Display(){
    override fun getColumns() = string.toByteArray().size
    override fun getRows() = 1
    override fun getRowText(row: Int): String? {
        return if (row == 0) {
            string
        } else {
            null
        }
    }
}
abstract class Border(protected val display: Display):Display(){


}
class SideBorder(display:Display,private val borderChar:Char):Border(display){
    override fun getColumns() = 1 + display.getColumns()
    override fun getRows() = display.getRows()
    override fun getRowText(row: Int): String? {
        return borderChar + display.getRowText(row).toString() + borderChar
    }
}
class FullBorder(display: Display) : Border(display) {
    override fun getColumns(): Int {
        return 1 + display.getColumns() + 1
    }

    override fun getRows(): Int {
        return 1 + display.getRows() + 1

    }

    override fun getRowText(row: Int): String? {
        return if (row == 0) {
            "+${makeLine('-',display.getColumns())}+"
        }else if (row == display.getRows() + 1) {
            "+${makeLine('-',display.getColumns())}+"
        } else {
            "|${display.getRowText(row-1)}|"
        }
    }
    private fun makeLine(ch: Char, count: Int):String {
        val buf = StringBuffer()
        for (i in 0 until count) {
            buf.append(ch)
        }
        return buf.toString()
    }
}

元のコードは以下の本からの引用です。サンプルコードは著者さんのサイトで配布されています。リンクはAmazonアソシエイトを使用しています。

増補改訂版Java言語で学ぶデザインパターン入門

ライセンス表記

Copyright (C) 2001,2004 Hiroshi Yuki.

hyuki@hyuki.com
This software is provided ‘as-is’, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.

コメント

タイトルとURLをコピーしました