Proxy:kotlin

気を付けたこと

  • kotlinでは複数のクラスを一つのファイルにまとめることが出来るのでまとめた
  • lateinitで宣言時に初期化する必要がなくなるがnullに似たようなエラーが出る恐れがある
  • セカンダリコンストラクタはプライマリコンストラクタを呼ばないとコンパイルエラーになる
  • Class宣言時に class classname {} のように()を省略すればプライマリコンストラクタが存在しないことになるので、プライマリコンストラクタを呼ばないセカンダリコンストラクタを定義できる
  • 文字列リテラルの中に”${式}”の形で変数や式を直接埋め込める

fun main(args: Array<String>) {
    val p = PrinterProxy("Alice")
    println("名前は現在${p.getPrinterName()}です")
    p.setPrinterName("Bob")
    println("名前は現在${p.getPrinterName()}デス")
    p.print("Hello, world.")
}
interface Printable {
    abstract fun setPrinterName(name: String)
    abstract fun getPrinterName():String
    abstract fun print(string:String)
}
class Printer:Printable{
    lateinit private var name:String

    constructor(name:String){
        this.name = name
        heavyJob("Printerのインスタンス(${name})を生成中")
    }
    constructor(){
        heavyJob("Printerのインスタンスを生成中")
    }
    override fun setPrinterName(name: String) {
        this.name = name
    }

    override fun getPrinterName(): String {
        return name
    }

    override fun print(string: String) {
        println("=== ${name} ===")
        println(string)
    }

    private fun heavyJob(msg: String) {
        print(msg)
        for (i in 0 until 5) {
            try {
                Thread.sleep(1000)
            } catch (e: InterruptedException) {
            }
            print(",")
        }
        println("完了")
    }
}
class PrinterProxy():Printable{
    lateinit private var name: String
    private var real:Printer? = null
    constructor(name:String):this(){
        this.name = name
    }

    @Synchronized
    override fun setPrinterName(name: String) {
        real?.setPrinterName(name)
        this.name = name
    }

    override fun getPrinterName(): String {
        return name
    }

    override fun print(string: String) {
        realize()
        real?.print(string)
    }

    @Synchronized
    fun realize() {
        if (real == null) {
            real = Printer(name)
        }
    }
}

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