Prototypeパターン
prototype == 原型/模範
abstract class Product : Cloneable {
abstract fun use(s: String)
fun createClone(): Product {
var p: Product? = null
try {
p = clone() as Product
} catch (e: CloneNotSupportedException) {
e.printStackTrace()
}
return requireNotNull(p, { "product must not be null" })
}
}
class Manager {
private val showCase = hashMapOf<String, Product>()
fun register(name: String, product: Product) {
showCase[name] = product
}
fun create(photoName: String): Product {
val p = showCase[photoName] as Product
return p.createClone()
}
}
class MessageBox(private var decoChar: String) : Product() {
override fun use(s: String) {
val length = s.toByteArray().size
repeat(length + 3) {
print(decoChar)
}
println("")
println("""$decoChar $s $decoChar""")
repeat(length + 3) {
print(decoChar)
}
println("")
}
}
class UnderLinePen(private var ulchar: String) : Product() {
override fun use(s: String) {
val length = s.toByteArray().size
println("¥\"" + s + "¥\"")
print(" ")
repeat(length - 1) {
print(ulchar)
}
println("")
}
}
fun main() {
val manager = Manager()
val upen = UnderLinePen("~")
val mbox = MessageBox("*")
val sbox = MessageBox("/")
manager.run {
register("strong message", upen)
register("warning box", mbox)
register("slash box", sbox)
create("strong message").use("Hello World")
create("warning box").use("Hello World")
create("slash box").use("Hello World")
}
}