Adapter:kotlin

気を付けたこと

  • kotlinでは複数のクラスを一つのファイルにまとめることが出来るのでまとめた

fun main(args: Array<String>) {
    val p = PrintBanner("Hello")
    p.printWeak()
    p.printStrong()
    val tp = transferPrintBanner("Hello")
    tp.printWeak()
    tp.printStrong()
}

open class Banner(private val string:String){
    fun showWithParen() {
        println("(${string})")
    }
    fun showWithAster() {
        println("*${string}*")
    }
}
interface Print{
    abstract fun printWeak()
    abstract fun printStrong()
}
class PrintBanner(string: String) : Banner(string), Print {
    override fun printWeak() {
        showWithParen()
    }

    override fun printStrong() {
        showWithAster()
    }

}
abstract class transferPrint{
    abstract fun printWeak()
    abstract fun printStrong()
}
class transferPrintBanner(string: String):transferPrint(){
    lateinit var banner:Banner
    init {
        this.banner = Banner(string)
    }

    override fun printWeak() {
        banner.showWithParen()
    }

    override fun printStrong() {
        banner.showWithAster()
    }

}

元のコードは以下の本からの引用です。サンプルコードは著者さんのサイトで配布されています。リンクは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をコピーしました