Skip to content

Commit eabe666

Browse files
committed
Pattern: Builder
1 parent d72e8fb commit eabe666

File tree

7 files changed

+181
-25
lines changed

7 files changed

+181
-25
lines changed

src/linuxMain/kotlin/org/dronix/kotlin/designpatterns/Main.kt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
package org.dronix.kotlin.designpatterns
22

3+
import org.dronix.kotlin.designpatterns.base.Maze
34
import org.dronix.kotlin.designpatterns.creational.abstractFactory.BombedMazeFactory
5+
import org.dronix.kotlin.designpatterns.creational.builder.CountingMazeBuilder
6+
import org.dronix.kotlin.designpatterns.creational.builder.StandardMazeBuilder
47

58
fun hello(): String = "Hello, Kotlin/Native Design Patterns!"
69

710
fun main(args: Array<String>) {
811

12+
val mazeBombed = createBombedMaze()
13+
val mazeStandard = createStandardMaze()
14+
val countingMaze = createCountingMaze()
15+
16+
println(hello())
17+
}
18+
19+
fun createBombedMaze(): Maze{
20+
921
val bombedMazeFactory = BombedMazeFactory()
1022
val mazeGame = MazeGame()
1123

12-
mazeGame.createMaze(bombedMazeFactory)
24+
return mazeGame.createMaze(bombedMazeFactory)
25+
}
26+
27+
fun createStandardMaze(): Maze{
28+
val standardMazeBuilder = StandardMazeBuilder()
29+
val mazeGame = MazeGame()
30+
31+
return mazeGame.createMaze(standardMazeBuilder)
32+
}
33+
34+
fun createCountingMaze(): Maze{
35+
val countingMazeBuilder = CountingMazeBuilder()
36+
val mazeGame = MazeGame()
37+
38+
val maze = mazeGame.createMaze(countingMazeBuilder)
39+
val (rooms, doors)= countingMazeBuilder.getCounts()
40+
println("Maze rooms: $rooms doors: $doors")
41+
return maze
42+
}
1343

14-
println(hello())
15-
}

src/linuxMain/kotlin/org/dronix/kotlin/designpatterns/MazeGame.kt

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,30 @@ import org.dronix.kotlin.designpatterns.base.door.Door
55
import org.dronix.kotlin.designpatterns.base.room.Room
66
import org.dronix.kotlin.designpatterns.base.wall.Wall
77
import org.dronix.kotlin.designpatterns.creational.abstractFactory.MazeFactory
8+
import org.dronix.kotlin.designpatterns.creational.builder.MazeBuilder
89

910
class MazeGame {
11+
fun createMaze(): Maze {
12+
val aMaze = Maze()
13+
val room1 = Room(1)
14+
val room2 = Room(2)
15+
val theDoor = Door(room1, room2)
16+
17+
aMaze.addRoom(room1)
18+
aMaze.addRoom(room2)
19+
20+
room1.setSide(Direction.North, Wall())
21+
room1.setSide(Direction.East, theDoor)
22+
room1.setSide(Direction.South, Wall())
23+
room1.setSide(Direction.West, Wall())
24+
25+
room2.setSide(Direction.North, Wall())
26+
room2.setSide(Direction.East, Wall())
27+
room2.setSide(Direction.South, Wall())
28+
room2.setSide(Direction.West, theDoor)
29+
return aMaze
30+
31+
}
1032

1133
fun createMaze(mazeFactory: MazeFactory): Maze{
1234

@@ -30,25 +52,20 @@ class MazeGame {
3052
return aMaze
3153
}
3254

33-
fun createMaze(): Maze {
34-
val aMaze = Maze()
35-
val room1 = Room(1)
36-
val room2 = Room(2)
37-
val theDoor = Door(room1, room2)
38-
39-
aMaze.addRoom(room1)
40-
aMaze.addRoom(room2)
41-
42-
room1.setSide(Direction.North, Wall())
43-
room1.setSide(Direction.East, theDoor)
44-
room1.setSide(Direction.South, Wall())
45-
room1.setSide(Direction.West, Wall())
46-
47-
room2.setSide(Direction.North, Wall())
48-
room2.setSide(Direction.East, Wall())
49-
room2.setSide(Direction.South, Wall())
50-
room2.setSide(Direction.West, theDoor)
51-
return aMaze
55+
fun createMaze(mazeBuilder: MazeBuilder): Maze {
56+
return mazeBuilder
57+
.buildMaze()
58+
.buildRoom(1)
59+
.buildRoom(2)
60+
.buildDoor(1, 2)
61+
.build()
62+
}
5263

64+
fun createComplexMaze(mazeBuilder: MazeBuilder): Maze {
65+
return mazeBuilder
66+
.buildMaze()
67+
.buildRoom(1)
68+
.buildRoom(1001)
69+
.build()
5370
}
5471
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
package org.dronix.kotlin.designpatterns.base.wall
22

3-
class BombedWall: Wall() {
4-
5-
}
3+
class BombedWall: Wall()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.dronix.kotlin.designpatterns.base.wall
2+
3+
import org.dronix.kotlin.designpatterns.base.Direction
4+
import org.dronix.kotlin.designpatterns.base.room.Room
5+
6+
object CommonWall{
7+
fun getDirection(room1: Room, room2: Room) : Direction{
8+
return Direction.East
9+
}
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.dronix.kotlin.designpatterns.creational.builder
2+
3+
import org.dronix.kotlin.designpatterns.base.Maze
4+
5+
class CountingMazeBuilder: MazeBuilder {
6+
private var doors: Int = 0
7+
private var rooms: Int = 0
8+
private var currentMaze: Maze? = null
9+
10+
override fun buildMaze(): MazeBuilder {
11+
currentMaze = Maze()
12+
doors = 0
13+
rooms = 0
14+
return this
15+
}
16+
17+
override fun buildRoom(roomNumber: Int): MazeBuilder {
18+
rooms++
19+
return this
20+
}
21+
22+
override fun buildDoor(roomFrom: Int, roomTo: Int): MazeBuilder {
23+
doors++
24+
return this
25+
}
26+
27+
public fun getCounts(): Count {
28+
return Count(this.rooms, this.doors)
29+
}
30+
31+
override fun build(): Maze {
32+
val maze = currentMaze
33+
return maze ?: Maze()
34+
}
35+
36+
}
37+
38+
data class Count(val rooms: Int, val doors: Int)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.dronix.kotlin.designpatterns.creational.builder
2+
3+
import org.dronix.kotlin.designpatterns.base.Maze
4+
5+
interface MazeBuilder {
6+
7+
fun buildMaze(): MazeBuilder
8+
fun buildRoom(roomNumber: Int): MazeBuilder
9+
fun buildDoor(roomFrom: Int, roomTo: Int): MazeBuilder
10+
11+
fun build(): Maze
12+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.dronix.kotlin.designpatterns.creational.builder
2+
3+
import org.dronix.kotlin.designpatterns.base.Direction
4+
import org.dronix.kotlin.designpatterns.base.Maze
5+
import org.dronix.kotlin.designpatterns.base.door.Door
6+
import org.dronix.kotlin.designpatterns.base.room.Room
7+
import org.dronix.kotlin.designpatterns.base.wall.CommonWall
8+
import org.dronix.kotlin.designpatterns.base.wall.Wall
9+
10+
class StandardMazeBuilder : MazeBuilder {
11+
private var currentMaze: Maze? = null
12+
13+
override fun buildMaze(): MazeBuilder {
14+
currentMaze = Maze()
15+
return this
16+
}
17+
18+
override fun buildRoom(roomNumber: Int): MazeBuilder {
19+
currentMaze = currentMaze ?: Maze()
20+
21+
if (currentMaze?.getRoomFromNumber(roomNumber) == null) {
22+
val room = Room(roomNumber)
23+
24+
currentMaze?.addRoom(room)
25+
26+
room.setSide(Direction.North, Wall())
27+
room.setSide(Direction.South, Wall())
28+
room.setSide(Direction.East, Wall())
29+
room.setSide(Direction.West, Wall())
30+
}
31+
return this
32+
}
33+
34+
override fun buildDoor(roomFrom: Int, roomTo: Int): MazeBuilder {
35+
currentMaze = currentMaze ?: Maze()
36+
37+
val room1 = currentMaze?.getRoomFromNumber(roomFrom)
38+
val room2 = currentMaze?.getRoomFromNumber(roomTo)
39+
40+
if (room1 != null && room2 != null) {
41+
val door = Door(room1, room2)
42+
43+
room1.setSide(CommonWall.getDirection(room1, room2), door)
44+
room2.setSide(CommonWall.getDirection(room2, room1), door)
45+
}
46+
47+
return this
48+
}
49+
50+
override fun build(): Maze {
51+
return currentMaze ?: Maze()
52+
}
53+
}

0 commit comments

Comments
 (0)