Repository navigation
Expand file tree
/
Copy pathJavaEmailExample2.java
More file actions
117 lines (93 loc) · 3.4 KB
/
Copy pathJavaEmailExample2.java
File metadata and controls
117 lines (93 loc) · 3.4 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package JavaInnerClassesPackage;
// Here we declare our second implementation of the EmailMessage. Note that we're now using
// the builder pattern to build the email message rather than the earlier, naive implementation.
class EmailMessage1 {
private String from;
private String to;
private String subject;
private String content;
private String mimeType;
// The outer class constructor is private and contains no statements. That's because we're never
// going to call the outer class constructor directly and instead do all the work through the static
//nested class called Builder.
private EmailMessage1() {}
public static class Builder {
// Instantiate the outer class directly from the Builder class.
private EmailMessage1 emailInstance = new EmailMessage1();
public Builder from (String f) {
// Add the from field to the emailInstance. Then return the Builder again.
System.out.println("In the from method of the Builder");
emailInstance.from = f;
return this;
}
public Builder to(String t) {
// Now add the to field, and again return the Builder.
System.out.println("In the to method of the Builder");
emailInstance.to = t;
return this;
}
public Builder subject(String s) {
// Add the subject, return the builder.
System.out.println("In the subject method of the Builder");
emailInstance.subject = s;
return this;
}
public Builder content (String c) {
// Add the content, return the builder.
System.out.println("In the content method of the Builder");
emailInstance.content = c;
return this;
}
public Builder mimeType (String mt) {
// Add the mimeType and return the builder.
System.out.println("In the mimetype method of the Builder");
emailInstance.mimeType = mt;
return this;
}
public EmailMessage1 build() {
// Finally, return the completely built emailInstance to the client application.
System.out.println("In the build method of the Builder");
return emailInstance;
}
}
public static Builder builder() {
// Construct the email message by calling the static nested class Builder.
System.out.println("In the builder method of the EmailMessage1 class");
return new EmailMessage1.Builder();
}
public String getFromField() {
return this.from;
}
public String getToField() {
return this.to;
}
public String getSubjectField() {
return this.subject;
}
public String getContentField() {
return this.content;
}
public String getMimeTypeField() {
return this.mimeType;
}
}
// This is the driver class for the example.
public class JavaEmailExample2 {
public static void main(String args[]) {
String content = "Dummy email";
String mimeType = "None";
// Here we see an example of using the builder design pattern when constructing our email.
EmailMessage1 em = EmailMessage1.builder()
.from("bbrelin@gmail.com")
.to("foo@bar.com")
.subject("Hi there")
.content(content)
.mimeType(mimeType)
.build();
System.out.println("From: " + em.getFromField());
System.out.println("To: " + em.getToField());
System.out.println("Subject: " + em.getSubjectField());
System.out.println("Content: " + em.getContentField());
System.out.println("MimeType: " + em.getMimeTypeField());
}
}