kotlin装饰者模式

/**
*装饰者模式
*/
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装饰者模式)