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 c3d0a79

Browse files
added Flyweight design pattern
1 parent 9a1304f commit c3d0a79

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package design_patterns
2+
3+
/**
4+
*
5+
* Flyweight is a structural design pattern that reduces memory costs by reusing a family of objects
6+
*
7+
* and storing mutable state outside the object.
8+
*
9+
*/
10+
11+
class BeautifulGarden {
12+
13+
// we reuse objects with immutable state, in this example trees
14+
private val existingTrees = mutableListOf<Tree>()
15+
16+
// mutable state (coordinates) are stored outside the object
17+
private val placedTrees = mutableListOf<Triple<Int, Int, Tree>>()
18+
19+
fun placeTree(name: String, description: String, height: Int, x: Int, y: Int) {
20+
// check if such a tree already exists reuse it otherwise create a new one
21+
val tree = existingTrees.find { tree -> tree.name == name && tree.description == description && tree.height == height }
22+
?: Tree(name, description, height).apply(existingTrees::add)
23+
24+
placedTrees.add(Triple(x, y, tree))
25+
}
26+
27+
fun placedTreesAsString(): String {
28+
val builder = StringBuilder()
29+
builder.append("Beautiful Garden:\n")
30+
31+
placedTrees.forEach { (x, y, tree) ->
32+
builder.append("name: ${tree.name}, height: ${tree.height}, x: $x, y: $y\n")
33+
}
34+
35+
builder.append("-|-|-|-|-|-|-|-|-|-|-")
36+
37+
return builder.toString()
38+
}
39+
40+
data class Tree(
41+
val name: String,
42+
val description: String,
43+
val height: Int
44+
)
45+
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package design_patterns
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class FlyweightTest {
7+
8+
@Test
9+
fun test() {
10+
val garden = BeautifulGarden()
11+
12+
garden.placeTree("oak", "", 21, 10, 10)
13+
garden.placeTree("birch", "", 15, 10, 20)
14+
garden.placeTree("birch", "", 16, 10, 30)
15+
garden.placeTree("birch", "", 17, 10, 40)
16+
garden.placeTree("oak", "", 21, 20, 10)
17+
garden.placeTree("oak", "", 21, 20, 20)
18+
garden.placeTree("oak", "", 21, 20, 30)
19+
garden.placeTree("birch", "", 15, 20, 40)
20+
21+
assertEquals("""
22+
Beautiful Garden:
23+
name: oak, height: 21, x: 10, y: 10
24+
name: birch, height: 15, x: 10, y: 20
25+
name: birch, height: 16, x: 10, y: 30
26+
name: birch, height: 17, x: 10, y: 40
27+
name: oak, height: 21, x: 20, y: 10
28+
name: oak, height: 21, x: 20, y: 20
29+
name: oak, height: 21, x: 20, y: 30
30+
name: birch, height: 15, x: 20, y: 40
31+
-|-|-|-|-|-|-|-|-|-|-
32+
""".trimIndent(), garden.placedTreesAsString())
33+
}
34+
35+
}

0 commit comments

Comments
(0)

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