げんとめブログ

ChainOfResponsibility:kotlin

気を付けたこと


fun main(args: Array<String>) {
    val alice = NoSupport("Alice")
    val bob = LimitSupport("bob", 100)
    val charlie = SpecialSupport("Charlie", 429)
    val diana = LimitSupport("Diana", 200)
    val elmo = OddSupport("Elmo")
    val fred = LimitSupport("Fred", 300)
    alice.setNext(bob)?.setNext(charlie)?.setNext(diana)?.setNext(elmo)?.setNext(fred)
    for (i in 0 until 500) {
        alice.support(Trouble(i))
    }
}
class Trouble(private val number:Int) {
    fun getNumber(): Int {
        return number
    }

    override fun toString(): String {
        return "$[Trouble${number}]"
    }


}
abstract class Support(private val name: String){
    private var next:Support? = null
    fun setNext(next: Support?) :Support?{
        this.next = next
        return next
    }
    fun support(trouble: Trouble) {
        if (resolve(trouble)) {
            done(trouble)
        }else if (next != null) {
            next?.support(trouble)
        } else {
            fail(trouble)
        }
    }
    override fun toString(): String {
        return "[${name}]"
    }
    abstract fun resolve(trouble: Trouble): Boolean
    protected fun done(trouble: Trouble) {
        println("${trouble} is resolved by ${this}")
    }
    protected fun fail(trouble: Trouble) {
        println("${trouble}cannot be resolved.")
    }
}
class NoSupport(name: String) : Support(name) {
    override fun resolve(trouble: Trouble): Boolean {
        return false
    }
}
class LimitSupport(name: String, private val limit: Int) : Support(name) {
    override fun resolve(trouble: Trouble): Boolean {
        if (trouble.getNumber() < limit) {
            return true
        } else {
            return false
        }
    }
}
class OddSupport(name: String) : Support(name) {
    override fun resolve(trouble: Trouble): Boolean {
        if (trouble.getNumber() % 2 == 1) {
            return true
        } else {
            return false
        }
    }
}
class SpecialSupport(name: String, private val number: Int) : Support(name) {
    override fun resolve(trouble: Trouble): Boolean {
        if (trouble.getNumber() == number) {
            return true
        } else {
            return false
        }
    }
}


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

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

ライセンス表記

Copyright (C) 2001,2004 Hiroshi Yuki.

『増補改訂版Java言語で学ぶデザインパターン入門』サポートページ
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.
Exit mobile version