一、装饰类
package com.study.decorator
interface IStar {
fun beautiful()
fun talentAndSkill()
}
package com.study.decorator
abstract class Decorator(private val star: IStar) : IStar {
override fun beautiful() {
star.beautiful()
}
override fun talentAndSkill() {
star.talentAndSkill()
}
}
package com.study.decorator
class BeautifulDecorator(star: IStar) : Decorator(star) {
override fun beautiful() {
makeUp()
super.beautiful()
}
private fun makeUp() {
println("通过化妆掩饰所有缺点,毕竟人无完人嘛")
}
}
package com.study.decorator
class TalentAndSkillDecorator(star: IStar) : Decorator(star) {
override fun talentAndSkill() {
train()
super.talentAndSkill()
}
private fun train() {
println("训练才艺,唱歌跳舞才能更吸引人嘛...")
}
}
二、被装饰类
package com.study.decorator
class YangChaoYue : IStar {
override fun beautiful() {
println("长得好看")
}
override fun talentAndSkill() {
println("唱歌跳舞")
}
}
三、主程序调用
package com.study.decorator
fun main() {
val yangChaoYue = YangChaoYue()
val beautifulDecorator = BeautifulDecorator(yangChaoYue)
val talentAndSkillDecorator = TalentAndSkillDecorator(yangChaoYue)
beautifulDecorator.beautiful()
talentAndSkillDecorator.talentAndSkill()
}
四、运行结果