06设计模式-kotlin-复制Prototype

什么是复制模式Prototype

一句话: 通过复制创建实例

直接上代码

简答的说就是实现了Clonable接口.

package prototype

import kotlin.math.min

/**
 * Created by silen on 2018/7/8 12:25
 * Copyright (c) 2018 in FORETREE
 */

// -*- 通过复制生成实例 -*-

//复制接口 ,注意这里的Product
interface Product {
    fun use(msg: String)
    fun cloneProduct(): Product?
}

//manager
class Manager {
    companion object {
        private val list = mutableMapOf()
        private var mInstance: Manager? = null
        fun getInstance():Manager {
            if (mInstance == null) {
                synchronized(Manager) {
                    if (mInstance == null) {
                        mInstance = Manager()
                    }
                }
            }
            return mInstance!!
        }

    }

    fun register(key: String, product: Product) {
        list[key] = product
    }

    fun create(key: String): Product {
        return list[key]!!.run {
            println("这是的实例===>$this")
            val cloneProduct = this.cloneProduct()
            println("这是复制的实例===>$cloneProduct")
            cloneProduct!!
        }
    }
}

class MessageBox : Product,Cloneable {
    override fun cloneProduct(): Product? {
        return try {
            clone() as Product
        } catch (e: CloneNotSupportedException) {
            e.printStackTrace()
            null
        }
    }

    override fun use(msg: String) {
        println("${this.javaClass.simpleName}的实例${msg}")
    }

}

class AnyPen : Product,Cloneable {
    override fun cloneProduct(): Product? {
        return try {
            clone() as Product
        } catch (e: CloneNotSupportedException) {
            e.printStackTrace()
            null
        }
    }

    override fun use(msg: String) {
        println("${this.javaClass.simpleName}的实例$msg")
    }
}

fun main(args: Array) {
    //创建两个实例
    val key1 = "message"
    val key2 = "pen"

    MessageBox().run {
        Manager.getInstance().register(key1, this)
    }

    AnyPen().run {
        Manager.getInstance().register(key2, this)
    }

    Manager.getInstance().create(key1).use("====> 消息")
    Manager.getInstance().create(key2).use("====> 消息")
}

运行结果

这是的实例===>prototype.MessageBox@63947c6b
这是复制的实例===>prototype.MessageBox@2b193f2d
MessageBox的实例====> 消息
这是的实例===>prototype.AnyPen@355da254
这是复制的实例===>prototype.AnyPen@4dc63996
AnyPen的实例====> 消息

注意

  • Clonable是很特殊的接口.
  • Clonable子类继承不能使用clone()方法,不然会报错...
    看上面的代码Product接口中如果实现了Clonable,子类没有实现这个接口,就会报错.
Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/Cloneable$DefaultImpls
    at prototype.Product$DefaultImpls.clone(Prototype.kt)
    at prototype.MessageBox.clone(Prototype.kt:50)
    at prototype.MessageBox.cloneProduct(Prototype.kt:53)
    at prototype.Manager.create(Prototype.kt:43)
    at prototype.PrototypeKt.main(Prototype.kt:94)

所以需要在子类实现Clonable接口就行了,就会复制不能的实例.看上面的运行结果.
就到这里,如果你有更好的想法加群一起聊聊....


06设计模式-kotlin-复制Prototype_第1张图片
QQ群:462546711

你可能感兴趣的:(06设计模式-kotlin-复制Prototype)