-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleMailer.java
31 lines (29 loc) · 1.08 KB
/
SampleMailer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
import java.util.function.*;
//Creating fluent interfaces using lamdas
//also called cascade method pattern -- train wreck pattern -- builder pattern
class Mailer {
public static void print(String msg) { System.out.println(msg); }
public Mailer from(String addr) { print("from"); return this;}
public Mailer to(String addr) { print("to"); return this;}
public Mailer subject(String line) { print("subject"); return this;}
public Mailer body(String msg) { print("body"); return this;}
public static void send(Consumer<Mailer> block) {
Mailer mailer = new Mailer();
block.accept(mailer);
System.out.println("sending...");
}
}
public class SampleMailer {
public static void main(String[] args) {
Mailer.send(mailer ->
mailer
.from("[email protected]")
.to("[email protected]")
.subject("Your code sucks")
.body("...here you go..."));
}
}
//making send method as higher order function
//by passing function as argument to a function -- pass a function get the data returned from a function
//