kotlin装饰者模式

作者: 腊鸭Laya | 来源:发表于2018-11-19 10:14 被阅读4次
/**
*装饰者模式
*/
abstract class Room {

abstract fun fitment()

}

class NewRoom : Room() {

override fun fitment() {

println("这是一间新房:装上电")

}

}


abstract class RoomDecorator(room: Room) : Room() {

private val mRoom = room

override fun fitment() {

mRoom.fitment()

}

}

class Bedroom(room: Room) : RoomDecorator(room) {

override fun fitment() {

super.fitment()

addBedding()

}

private fun addBedding() {

println("装修成卧室:添加卧具")

}

}

class Kitchen(room: Room) : RoomDecorator(room) {

override fun fitment() {

super.fitment()

addKitchenware()

}

private fun addKitchenware() {

println("装修成厨房:添加厨具")

}

}

fun main(args: Array) {

val newRoom: Room = NewRoom();//有一间新房间

    val bedroom: RoomDecorator = Bedroom(newRoom);

bedroom.fitment();//装修成卧室

    val kitchen: RoomDecorator = Kitchen(newRoom);

kitchen.fitment();//装修成厨房

}

相关文章

  • kotlin装饰者模式

  • Kotlin Extension — Method

    简单来说,Extension就是Kotlin版的 Decorator(装饰者模式) 【Example】当我们使用E...

  • 如何利用装饰者模式在不改变原有对象的基础上扩展功能

    目录 什么是装饰者模式 普通示例 装饰者模式示例 类图关系 装饰者模式使用场景 装饰者模式优点 装饰者模式缺点 什...

  • kotlin基础(三)

    kotlin扩展函数 Kotlin 可以对一个类的属性和方法进行扩展,且不需要继承或使用 装饰者模式。定义形式:f...

  • 装饰者模式

    装饰者模式 装饰者模式和适配器模式对比 装饰者模式 是一种特别的适配器模式 装饰者与被装饰者都要实现同一个接口,主...

  • java IO 的知识总结

    装饰者模式 因为java的IO是基于装饰者模式设计的,所以要了解掌握IO 必须要先清楚什么事装饰者模式(装饰者模式...

  • 设计模式-装饰者模式

    装饰者模式概念: 装饰者模式又名包装(Wrapper)模式。装饰者模式以对客户端透明的方式扩展对象的功能,是继承关...

  • java - 装饰者模式

    装饰者模式 装饰者模式:动态将责任添加到对象上。如果需要扩展功能,装饰者提供了比继承更有弹性的解决方案。装饰者模式...

  • 设计模式之装饰者模式(Decorator Pattern)

    What: 装饰者模式又名包装(Wrapper)模式。装饰者模式动态地将责任附加到对象身上。若要扩展功能,装饰者提...

  • 装饰者(Decorator)模式

    装饰者(Decorator)模式装饰模式又名包装(Wrapper)模式。装饰模式是继承关系的一个替代方案。装饰模式...

网友评论

    本文标题:kotlin装饰者模式

    本文链接:https://www.haomeiwen.com/subject/wzrofqtx.html