Skip to content

Commit f710638

Browse files
committed
wip
1 parent 2318329 commit f710638

File tree

6 files changed

+67
-35
lines changed

6 files changed

+67
-35
lines changed

src/main/java/j/template_method/like/functions/Display.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Display(Supplier<Void> opener, Supplier<Void> printer, Supplier<Void> clo
1313
this.closer = closer;
1414
}
1515

16-
public final void display() {
16+
public void display() {
1717
opener.get();
1818
for (int i = 0; i < 5; i++) {
1919
printer.get();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package k.template_method.like.functions
2+
3+
class Display(
4+
private val opener: () -> Unit,
5+
private val printer: () -> Unit,
6+
private val closer: () -> Unit
7+
) {
8+
fun display() {
9+
opener()
10+
repeat(5) {
11+
printer()
12+
}
13+
closer()
14+
}
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package k.template_method.like.functions
2+
3+
object DisplayFactory {
4+
5+
fun createString(string: String): Display {
6+
val width = string.length
7+
8+
fun printLine() {
9+
print("+")
10+
repeat(width) {
11+
print("-")
12+
}
13+
println("+")
14+
}
15+
16+
val opener = { printLine() }
17+
val printer = { println("|$string|") }
18+
val closer = { printLine() }
19+
20+
return Display(opener, printer, closer)
21+
}
22+
23+
fun createChar(ch: Char): Display {
24+
val opener = { print("<<") }
25+
val printer = { print(ch) }
26+
val closer = { println(">>") }
27+
28+
return Display(opener, printer, closer)
29+
}
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package k.template_method.like.functions
2+
3+
object Main {
4+
@JvmStatic
5+
fun main(args: Array<String>) {
6+
val d1 = DisplayFactory.createChar('H')
7+
val d2 = DisplayFactory.createString("Hello, world.")
8+
9+
d1.display()
10+
d2.display()
11+
}
12+
}

src/main/scala/s/template_method/like/functional/Display.scala

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package s.template_method.like.functional
22

3-
import s.template_method.like.functional.Display._
4-
5-
object Display {
6-
type DisplayDelegate = () => Unit
7-
}
8-
93
final class Display(
10-
private val opener: DisplayDelegate,
11-
private val printer: DisplayDelegate,
12-
private val closer: DisplayDelegate
4+
opener: () => Unit,
5+
printer: () => Unit,
6+
closer: () => Unit
137
) {
148
def display(): Unit = {
159
opener()

src/main/scala/s/template_method/like/functional/DisplayFactory.scala

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package s.template_method.like.functional
22

3-
import s.template_method.like.functional.Display.DisplayDelegate
4-
53
object DisplayFactory {
64

75
def createString(string: String): Display = {
@@ -15,34 +13,17 @@ object DisplayFactory {
1513
Console.println("+")
1614
}
1715

18-
val opener: DisplayDelegate = () => {
19-
printLine()
20-
}
21-
22-
val printer: DisplayDelegate = () => {
23-
Console.println("|" + string + "|")
24-
}
25-
26-
val closer: DisplayDelegate = () => {
27-
printLine()
28-
}
16+
val opener = () => printLine()
17+
val printer = () => Console.println("|" + string + "|")
18+
val closer = () => printLine()
2919

3020
new Display(opener, printer, closer)
3121
}
3222

3323
def createChar(ch: Char): Display = {
34-
val opener: DisplayDelegate = () => {
35-
Console.print("<<")
36-
}
37-
38-
val printer: DisplayDelegate = () => {
39-
Console.print(ch)
40-
}
41-
42-
val closer: DisplayDelegate = () => {
43-
Console.println(">>")
44-
}
45-
24+
val opener = () => Console.print("<<")
25+
val printer = () => Console.print(ch)
26+
val closer = () => Console.println(">>")
4627
new Display(opener, printer, closer)
4728
}
4829

0 commit comments

Comments
 (0)