-
Notifications
You must be signed in to change notification settings - Fork 108
/
Composite.kt
53 lines (42 loc) · 1.46 KB
/
Composite.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package design_patterns
/**
*
* Composite is a structural design pattern that organizes objects into a tree structure
*
* and allows clients to access individual objects and groups of objects in the same way.
*
*/
// all components have a common interface
abstract class MenuComponent(val title: String) {
// menus and items can give information about themselves
abstract fun fetchMenuInformation() : String
}
class Menu(title: String) : MenuComponent(title) {
// menu can contain other menus and items
private val childMenuComponents = mutableListOf<MenuComponent>()
// addComponent/removeComponent operations are only available for the menu
fun addComponent(component: MenuComponent) {
childMenuComponents.add(component)
}
fun removeComponent(component: MenuComponent) {
childMenuComponents.remove(component)
}
override fun fetchMenuInformation(): String {
val builder = StringBuilder()
builder.append("Menu: $title")
childMenuComponents.forEach { component ->
builder.append("\n")
builder.append(component.fetchMenuInformation())
}
return builder.toString()
}
}
// the simple component that contains no others
class MenuItem(title: String, private val price: Int) : MenuComponent(title) {
override fun fetchMenuInformation(): String =
"""
title: $title
price: $price
-------------
""".trimIndent()
}