Skip to content

Commit b51ec90

Browse files
Added Builder Pattern
1 parent ac1d248 commit b51ec90

5 files changed

Lines changed: 186 additions & 0 deletions

File tree

Builder/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.gradle
2+
/build/
3+
/bin/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
6+
### STS ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
15+
### IntelliJ IDEA ###
16+
.idea
17+
*.iws
18+
*.iml
19+
*.ipr
20+
/out/
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/

Builder/build.gradle

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/********** Applying Plugin *************/
2+
apply plugin: 'java'
3+
apply plugin: 'eclipse'
4+
5+
/********** buildscript *************/
6+
buildscript {
7+
ext {
8+
version = '2.0.1.RELEASE'
9+
}
10+
repositories {
11+
mavenCentral()
12+
}
13+
dependencies {
14+
15+
}
16+
}
17+
18+
repositories {
19+
mavenCentral()
20+
}
21+
22+
sourceCompatibility = 1.7
23+
group = 'com.java.design.pattern'
24+
version = '0.0.1-SNAPSHOT'
25+
26+
println "Starting Building Project:"+project.name
27+
28+
jar
29+
{
30+
baseName=project.name
31+
32+
}
33+
34+
35+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.java.design.pattern.builder;
2+
3+
public class TestBuilder {
4+
5+
6+
public static void main(String[] args) {
7+
8+
User user1=new User.UserBuilder()
9+
.firstName("Rahat")
10+
.lastName("Ali")
11+
.age(31)
12+
.address("Pune")
13+
.occupation("Programmer")
14+
.mobile("8765434567")
15+
//.maritalStatus("Married")
16+
.build();
17+
System.out.println(user1.toString());
18+
19+
}
20+
21+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.java.design.pattern.builder;
2+
3+
/*To build the builder, we must have all attributes should be private
4+
There must be inner static class
5+
constructer of outer class which accept the instance of inner class.
6+
*/
7+
public class User {
8+
9+
private String firstName;
10+
private String lastName;
11+
private String address;
12+
private int age;
13+
private String mobile;
14+
private String maritalStatus;
15+
private String occupation;
16+
17+
18+
19+
20+
private User(UserBuilder userBuilder)
21+
{
22+
this.firstName=userBuilder.fname;
23+
this.lastName=userBuilder.lname;
24+
this.address=userBuilder.address;
25+
this.age=userBuilder.age;
26+
this.mobile=userBuilder.mobile;
27+
this.maritalStatus=userBuilder.maritalStatus;
28+
this.occupation=userBuilder.occupation;
29+
}
30+
31+
32+
33+
@Override
34+
public String toString() {
35+
return "User [firstName=" + firstName + ", lastName=" + lastName + ", address=" + address + ", age=" + age
36+
+ ", mobile=" + mobile + ", maritalStatus=" + maritalStatus + ", occupation=" + occupation + "]";
37+
}
38+
39+
40+
41+
public static class UserBuilder
42+
{
43+
private String fname;
44+
private String lname;
45+
private String address;
46+
private int age;
47+
private String mobile;
48+
private String maritalStatus;
49+
private String occupation;
50+
51+
public UserBuilder firstName(String lastName)
52+
{
53+
this.fname=lastName;
54+
return this;
55+
}
56+
57+
public UserBuilder lastName(String lastName)
58+
{
59+
this.lname=lastName;
60+
return this;
61+
}
62+
public UserBuilder address(String address)
63+
{
64+
this.address=address;
65+
return this;
66+
}
67+
public UserBuilder age(int age)
68+
{
69+
this.age=age;
70+
return this;
71+
}
72+
public UserBuilder mobile(String mobile)
73+
{
74+
this.mobile=mobile;
75+
return this;
76+
}
77+
public UserBuilder maritalStatus(String maritalStatus)
78+
{
79+
this.maritalStatus=maritalStatus;
80+
return this;
81+
}
82+
83+
public UserBuilder occupation(String occupation)
84+
{
85+
this.occupation=occupation;
86+
return this;
87+
}
88+
89+
public User build() {
90+
User user=new User(this);
91+
//validateUserObject(user);
92+
return user;
93+
}
94+
95+
private void validateUserObject(User user) {
96+
//Do some basic validations to check
97+
//if user object does not break any assumption of system
98+
}
99+
100+
}
101+
102+
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rootProject.name = 'DesignPattern'
22
include 'Singelton'
33
include 'Factory'
4+
include 'Builder'
45

0 commit comments

Comments
 (0)