-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilderDesignPattern
More file actions
90 lines (85 loc) · 1.66 KB
/
BuilderDesignPattern
File metadata and controls
90 lines (85 loc) · 1.66 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
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class DP {
String firstName;
String lastname;
String middleName;
String postalCode;
String address;
String phoneNumber;
DP() {
firstName="";
lastname="";
middleName="";
postalCode="";
address="";
phoneNumber="";
}
DP(Builder b) {
firstName=b.firstName;
lastname=b.lastname;
middleName=b.middleName;
postalCode=b.postalCode;
address=b.address;
phoneNumber=b.phoneNumber;
}
@Override
public String toString() {
String ret = "";
if(!firstName.equals("")) {
ret+=firstName +" ";
}
else if(!lastname.equals("")) {
ret+=lastname +" ";
}
return ret;
}
static class Builder {
String firstName="";
String lastname="";
String middleName="";
String postalCode="";
String address="";
String phoneNumber="";
Builder() {
}
public DP build() {
return new DP(this);
}
public Builder setFirstName(String str) {
firstName = str;
return this;
}
public Builder setLastName(String str) {
lastname = str;
return this;
}
public Builder setMiddleName(String str) {
middleName = str;
return this;
}
public Builder sePostalCode(String str) {
postalCode = str;
return this;
}
public Builder setAddress(String str) {
address = str;
return this;
}
public Builder setPhoneNumber(String str) {
phoneNumber = str;
return this;
}
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
DP o1 = new DP.Builder().setFirstName("Abhinav").build();
System.out.println(o1);
}
}