回答編集履歴
1
追記
answer
CHANGED
@@ -33,4 +33,34 @@
|
|
33
33
|
- 『何かを書く』一連の処理を持つ役割
|
34
34
|
- 処理同士を結合する役割
|
35
35
|
|
36
|
-
せめてappendメソッドはstaticにした方が分かり易いと思うんですがね。
|
36
|
+
せめてappendメソッドはstaticにした方が分かり易いと思うんですがね。
|
37
|
+
こんなイメージで。
|
38
|
+
```Java
|
39
|
+
@FunctionalInterface
|
40
|
+
interface Drawable {
|
41
|
+
void draw(String str);
|
42
|
+
|
43
|
+
static Drawable concatenate(Drawable... tasks) {
|
44
|
+
return (str) -> {
|
45
|
+
for(Drawable task: tasks) { task.draw(str); }
|
46
|
+
};
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
class Main {
|
51
|
+
public static void main(String[] args) {
|
52
|
+
Drawable task1 = (str) -> System.out.println("task1: " + str);
|
53
|
+
Drawable task2 = (str) -> System.out.println("task2: " + str);
|
54
|
+
Drawable task3 = (str) -> System.out.println("task3: " + str);
|
55
|
+
|
56
|
+
Drawable.concatenate(task1, task2, task3).draw("hoge");
|
57
|
+
}
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/AMhxzA5kTpzFDstU)
|
62
|
+
```
|
63
|
+
task1: hoge
|
64
|
+
task2: hoge
|
65
|
+
task3: hoge
|
66
|
+
```
|