Skip to content

Commit f77deef

Browse files
committed
Builder Design Pattern
1 parent d0fc54a commit f77deef

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Getting Started
2+
3+
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
4+
5+
## Folder Structure
6+
7+
The workspace contains two folders by default, where:
8+
9+
- `src`: the folder to maintain sources
10+
- `lib`: the folder to maintain dependencies
11+
12+
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
13+
14+
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
15+
16+
## Dependency Management
17+
18+
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class App {
2+
public static void main(String[] args) {
3+
4+
User user1 = User.UserBuilder.builder()
5+
.setUserId(101)
6+
.setUsername("dhirajgadekar")
7+
.setEmailId("dhirajgadekar@gmail.com")
8+
.build();
9+
10+
User user2 = User.UserBuilder.builder()
11+
.setUserId(102)
12+
.setUsername("dhiraj")
13+
.build();
14+
15+
User user3 = User.UserBuilder.builder()
16+
.setEmailId("gadekar@gmail.com")
17+
.setUsername("gadekar")
18+
.setUserId(103)
19+
.build();
20+
21+
System.out.println(user1);
22+
System.out.println(user2);
23+
System.out.println(user3);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class User {
2+
3+
private final int userId;
4+
private final String username;
5+
private final String emailId;
6+
private User(UserBuilder buider) {
7+
8+
this.userId = buider.userId;
9+
this.username = buider.username;
10+
this.emailId = buider.emailId;
11+
}
12+
public int getUserId() {
13+
return userId;
14+
}
15+
public String getUsername() {
16+
return username;
17+
}
18+
public String getEmailId() {
19+
return emailId;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
25+
return this.userId + " : " + this.username + " : " + this.emailId;
26+
}
27+
28+
static class UserBuilder {
29+
30+
private int userId;
31+
private String username;
32+
private String emailId;
33+
34+
private UserBuilder() {
35+
36+
}
37+
38+
public static UserBuilder builder() {
39+
40+
return new UserBuilder();
41+
}
42+
public UserBuilder setUserId(int userId) {
43+
this.userId = userId;
44+
return this;
45+
}
46+
47+
public UserBuilder setUsername(String username) {
48+
this.username = username;
49+
return this;
50+
}
51+
52+
public UserBuilder setEmailId(String emailId) {
53+
this.emailId = emailId;
54+
return this;
55+
}
56+
57+
public User build() {
58+
59+
User user = new User(this);
60+
return user;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)