Skip to content

Commit 507a4e4

Browse files
committed
class 20 chaining
1 parent 82a97e1 commit 507a4e4

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • modules/src/main/java/com/platzi/functional_student_practice/_10_chaining
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.platzi.functional_student_practice._10_chaining;
2+
3+
4+
public class C20Chaining {
5+
public static void main(String[] args) {
6+
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
7+
System.out.println("\nCLASS 20\n");
8+
9+
// 1. Every .append() method returns "this" object with the changes updated in state.
10+
StringBuilder stringBuilder = new StringBuilder();
11+
stringBuilder.append("Hola ")
12+
.append("alumno")
13+
.append("!")
14+
.reverse()
15+
.append(" :iztalP")
16+
.reverse();
17+
System.out.println(stringBuilder);
18+
19+
// 2. Custom example. This is usefull with FUNCTION COMPOSITION
20+
Chainer chainer = new Chainer();
21+
Chainer chainer2 = chainer.sayHi();
22+
Chainer chainer3 = chainer2.sayBye();
23+
chainer3.sayHi().sayBye();
24+
}
25+
26+
private static class Chainer {
27+
public Chainer sayHi() {
28+
System.out.println("Hola");
29+
return this;
30+
}
31+
32+
public Chainer sayBye() {
33+
System.out.println("Bye");
34+
return this;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)