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 2e3389c

Browse files
added Bridge design pattern
1 parent 92c70bf commit 2e3389c

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package design_patterns
2+
3+
/**
4+
*
5+
* Bridge is a structural design pattern used to separate abstraction and implementation
6+
*
7+
* so they can change independently
8+
*
9+
*/
10+
11+
// OperatingSystem implements an internal algorithm for drawing primitives (operating systems are implementations in this example)
12+
abstract class OperatingSystem(private val name: String, private val version: Int) {
13+
14+
protected val lines = mutableListOf<String>()
15+
protected val circles = mutableListOf<String>()
16+
17+
abstract fun drawLine(startX: Int, startY: Int, endX: Int, endY: Int)
18+
19+
abstract fun drawCircle(centerX: Int, centerY: Int, radius: Int)
20+
21+
override fun toString(): String {
22+
return """
23+
$name $version
24+
Lines:
25+
${lines.joinToString("\n")}
26+
Circles:
27+
${circles.joinToString("\n")}
28+
""".trimIndent()
29+
}
30+
31+
}
32+
33+
// different implementations have different algorithms for drawing primitives
34+
class WindowsSystem : OperatingSystem("Windows", 10) {
35+
36+
override fun drawLine(startX: Int, startY: Int, endX: Int, endY: Int) {
37+
lines.add("Windows graphic subsystem -> startX: $startX, startY: $startY, endX: $endX, endY: $endY")
38+
}
39+
40+
override fun drawCircle(centerX: Int, centerY: Int, radius: Int) {
41+
circles.add("Windows graphic subsystem -> centerX: $centerX, centerY: $centerY, radius: $radius")
42+
}
43+
44+
}
45+
46+
class MacOSSystem : OperatingSystem("MacOS", 14) {
47+
48+
override fun drawLine(startX: Int, startY: Int, endX: Int, endY: Int) {
49+
lines.add("MacOS graphic subsystem -> startX: $startX, startY: $startY, endX: $endX, endY: $endY")
50+
}
51+
52+
override fun drawCircle(centerX: Int, centerY: Int, radius: Int) {
53+
circles.add("MacOS graphic subsystem -> centerX: $centerX, centerY: $centerY, radius: $radius")
54+
}
55+
56+
}
57+
58+
// we can add new primitives without changing OperatingSystem implementations (primitives are abstractions in this example)
59+
abstract class GraphicPrimitive {
60+
abstract fun draw(system: OperatingSystem)
61+
}
62+
63+
class GraphicCirclePrimitive(
64+
private val centerX: Int,
65+
private val centerY: Int,
66+
private val radius: Int
67+
) : GraphicPrimitive() {
68+
69+
override fun draw(system: OperatingSystem) {
70+
system.drawCircle(centerX, centerY, radius)
71+
}
72+
73+
}
74+
75+
class GraphicLinePrimitive(
76+
private val startX: Int,
77+
private val startY: Int,
78+
private val endX: Int,
79+
private val endY: Int
80+
) : GraphicPrimitive() {
81+
82+
override fun draw(system: OperatingSystem) {
83+
system.drawLine(startX, startY, endX, endY)
84+
}
85+
86+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package design_patterns
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class BridgeTest {
7+
8+
@Test
9+
fun test() {
10+
val windowsSystem = WindowsSystem()
11+
val macOSSystem = MacOSSystem()
12+
13+
// the line doesn't know how graphics are implemented in different operating systems
14+
val line = GraphicLinePrimitive(0, 0, 100, 100)
15+
line.draw(windowsSystem)
16+
line.draw(macOSSystem)
17+
18+
// the circle doesn't know how graphics are implemented in different operating systems
19+
val circle = GraphicCirclePrimitive(10, 10, 6)
20+
circle.draw(windowsSystem)
21+
circle.draw(macOSSystem)
22+
23+
assertEquals("""
24+
Windows 10
25+
Lines:
26+
Windows graphic subsystem -> startX: 0, startY: 0, endX: 100, endY: 100
27+
Circles:
28+
Windows graphic subsystem -> centerX: 10, centerY: 10, radius: 6
29+
""".trimIndent(), windowsSystem.toString())
30+
31+
assertEquals("""
32+
MacOS 14
33+
Lines:
34+
MacOS graphic subsystem -> startX: 0, startY: 0, endX: 100, endY: 100
35+
Circles:
36+
MacOS graphic subsystem -> centerX: 10, centerY: 10, radius: 6
37+
""".trimIndent(), macOSSystem.toString())
38+
}
39+
40+
}

0 commit comments

Comments
(0)

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