30
loading...
This website collects cookies to deliver better user experience
abstract class Solid {
abstract val description: String
abstract val price: Int
}
class Eba : Solid() {
override val description: String
get() = "Eba"
override val price: Int
get() = 100
}
class Fufu : Solid(){
override val description: String
get() = "Fufu"
override val price: Int
get() = 200
}
abstract class SolidDecorator(val solid: Solid) : Solid() {
abstract override val description: String
}
class NativeSoupDecorator(solid: Solid) : SolidDecorator(solid) {
override val description: String
get() = "${solid.description} with Native Soup"
override val price: Int
get() = solid.price + 500
}
class AfangSoupDecorator(solid: Solid) : SolidDecorator(solid) {
override val description: String
get() = "${solid.description} with Afang Soup"
override val price: Int
get() = solid.price + 500
}
SolidDecorator
abstract class. It has a reference to a Solid
class and it also subclasses Solid
. This is what makes Decorators special. You can then create many concrete classes of the SolidDecorator
adding new behaviours to the initial wrapped class. Decorators can also add their behaviour before and/or after delegating actions to the target object.var order : Solid = Eba()
order = NativeSoupDecorator(solid = order)
order = AfangSoupDecorator(solid = order)
order = FishDecorator(order)
println("${order.description} price: ${order.price}")
var order2 : Solid = Eba()
order2 = NativeSoupDecorator(solid = order2)
order2 = GoatMeatDecorator(solid = order2)
println("${order2.description} price: ${order2.price}")
Eba, Native Soup, Afang Soup, goat meat price: 2100
Eba, Native Soup, goat meat price: 650