Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50ca620

Browse files
added Composite design pattern
1 parent 2460d47 commit 50ca620

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package design_patterns
2+
3+
/**
4+
*
5+
* Composite is a structural design pattern that organizes objects into a tree structure
6+
*
7+
* and allows clients to access individual objects and groups of objects in the same way.
8+
*
9+
*/
10+
11+
// all components have a common interface
12+
abstract class MenuComponent(val title: String) {
13+
// menus and items can give information about themselves
14+
abstract fun fetchMenuInformation() : String
15+
}
16+
17+
class Menu(title: String) : MenuComponent(title) {
18+
19+
// menu can contain other menus and items
20+
private val childMenuComponents = mutableListOf<MenuComponent>()
21+
22+
// addComponent/removeComponent operations are only available for the menu
23+
fun addComponent(component: MenuComponent) {
24+
childMenuComponents.add(component)
25+
}
26+
27+
fun removeComponent(component: MenuComponent) {
28+
childMenuComponents.remove(component)
29+
}
30+
31+
override fun fetchMenuInformation(): String {
32+
val builder = StringBuilder()
33+
builder.append("Menu: $title")
34+
childMenuComponents.forEach { component ->
35+
builder.append("\n")
36+
builder.append(component.fetchMenuInformation())
37+
}
38+
return builder.toString()
39+
}
40+
41+
}
42+
43+
// the simple component that contains no others
44+
class MenuItem(title: String, private val price: Int) : MenuComponent(title) {
45+
46+
override fun fetchMenuInformation(): String =
47+
"""
48+
title: $title
49+
price: $price
50+
-------------
51+
""".trimIndent()
52+
53+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package design_patterns
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class CompositeTest {
7+
8+
@Test
9+
fun test() {
10+
val menu = Menu("Delicious Restaurant")
11+
12+
val pizzasMenu = Menu("Pizzas")
13+
pizzasMenu.addComponent(MenuItem("Cheese pizza", 10))
14+
pizzasMenu.addComponent(MenuItem("Pepperoni pizza", 11))
15+
menu.addComponent(pizzasMenu)
16+
17+
val cakesMenu = Menu("Cakes")
18+
cakesMenu.addComponent(MenuItem("Chocolate cake", 13))
19+
cakesMenu.addComponent(MenuItem("Cheese cake", 13))
20+
menu.addComponent(cakesMenu)
21+
22+
assertEquals("""
23+
Menu: Delicious Restaurant
24+
Menu: Pizzas
25+
title: Cheese pizza
26+
price: 10
27+
-------------
28+
title: Pepperoni pizza
29+
price: 11
30+
-------------
31+
Menu: Cakes
32+
title: Chocolate cake
33+
price: 13
34+
-------------
35+
title: Cheese cake
36+
price: 13
37+
-------------
38+
""".trimIndent(), menu.fetchMenuInformation())
39+
}
40+
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /