Skip to content

Commit 5e3ec54

Browse files
committed
builder pattern added
2 parents 6967ca0 + 2c50acf commit 5e3ec54

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ConnectionPool {
2323
private static ConnectionPool pool=new ConnectionPool();
2424
//this is a pool with only one connection
2525
private Connection connection=new Connection();
26+
2627
private ConnectionPool() {}
2728
//add a static method to get an instance of a singleton class
2829
public static ConnectionPool getPool() {

bin/builder/Application.class

1.2 KB
Binary file not shown.

bin/builder/Customer$Builder.class

1.7 KB
Binary file not shown.

bin/builder/Customer.class

1.55 KB
Binary file not shown.

src/builder/Application.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package builder;
2+
3+
public class Application {
4+
5+
public static void main(String[] args) {
6+
7+
8+
Customer customer1=new Customer.Builder()
9+
.withFirstName("Joe")
10+
.withLastName("Doe")
11+
.withEmail("joe22@")
12+
.withAge(22)
13+
.isMarried(false)
14+
.withShoeSize(6)
15+
.build();
16+
System.out.println(customer1);
17+
18+
Customer customer2=new Customer.Builder()
19+
.withAge(33).isMarried(true).withYearlyAmountSpendOnShoes(44444.0).build();
20+
System.out.println(customer2);
21+
22+
}
23+
24+
}

src/builder/Customer.java

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package builder;
2+
3+
public class Customer {
4+
5+
private String firstName;
6+
private String lastName;
7+
private String email;
8+
private int age;
9+
private int shoeSize;
10+
private double yearlyIncome;
11+
private double yearlyAmountSpendOnShoes;
12+
private boolean isMarried;
13+
14+
public static class Builder{
15+
16+
private String firstName="";
17+
private String lastName="";
18+
private String email="";
19+
private int age=0;
20+
private int shoeSize=0;
21+
private double yearlyIncome=0;
22+
private double yearlyAmountSpendOnShoes=0.0;
23+
private boolean isMarried=false;
24+
25+
public Builder withFirstName(String firstName) {
26+
this.firstName=firstName;
27+
return this;
28+
}
29+
30+
public Builder withLastName(String lastName) {
31+
this.lastName=lastName;
32+
return this;
33+
}
34+
35+
public Builder withEmail(String email) {
36+
this.email=email;
37+
return this;
38+
}
39+
public Builder withAge(int age) {
40+
this.age=age;
41+
return this;
42+
43+
}
44+
public Builder withShoeSize(int shoeSize) {
45+
this.shoeSize=shoeSize;
46+
return this;
47+
}
48+
public Builder withYearlyIncome(int yearlyIncome) {
49+
this.yearlyIncome=yearlyIncome;
50+
return this;
51+
}
52+
public Builder withYearlyAmountSpendOnShoes(double yearlyAmountSpendOnShoes)
53+
{
54+
this.yearlyAmountSpendOnShoes=yearlyAmountSpendOnShoes;
55+
return this;
56+
}
57+
58+
public Builder isMarried(boolean isMarried){
59+
this.isMarried=true;
60+
return this;
61+
}
62+
63+
public Customer build() {
64+
return new Customer(this);
65+
}
66+
67+
}
68+
69+
70+
private Customer(Builder builder) {
71+
72+
this.firstName=builder.firstName;
73+
this.lastName=builder.lastName;
74+
this.email=builder.email;
75+
this.age=builder.age;
76+
this.shoeSize=builder.shoeSize;
77+
this.yearlyIncome=builder.yearlyIncome;
78+
this.yearlyAmountSpendOnShoes=builder.yearlyAmountSpendOnShoes;
79+
this.isMarried=builder.isMarried;
80+
81+
}
82+
83+
84+
@Override
85+
public String toString() {
86+
return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + ", age=" + age
87+
+ ", shoeSize=" + shoeSize + ", yearlyIncome=" + yearlyIncome + ", yearlyAmountSpendOnShoes="
88+
+ yearlyAmountSpendOnShoes + ", isMarried=" + isMarried + "]";
89+
}
90+
91+
92+
}

0 commit comments

Comments
 (0)