https://github.com/husna-poyraz/builder_design_pattern
Builder design pattern and builder annotation
https://github.com/husna-poyraz/builder_design_pattern
annatation builder-design-pattern contruction java lombok
Last synced: about 1 year ago
JSON representation
Builder design pattern and builder annotation
- Host: GitHub
- URL: https://github.com/husna-poyraz/builder_design_pattern
- Owner: Husna-POYRAZ
- Created: 2023-02-19T12:37:03.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-19T13:05:16.000Z (about 3 years ago)
- Last Synced: 2025-01-09T04:21:53.835Z (over 1 year ago)
- Topics: annatation, builder-design-pattern, contruction, java, lombok
- Language: Java
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Builder Design Pattern
Suppose there is more than one field belonging to an object in object-oriented programming.
```
@Data
@AllArgsConstructor
public class Country {
private String id;
private String name;
private String nativeName;
private int phoneCode;
private String continent;
private String capital;
private String currency;
private List languages;
}
```
When creating the object, we pass the objects that we do not want to give a value as null.
```
Country country1 = new Country("TR", "Turkey", null, 0, null, null, null, null);
```
As the number of parameters increases, the readability will decrease. The probability of error will increase. With the Builder design pattern, if you want to set a field to null, you don't need to do anything. We can also use it like this by adding the @Builder annotation.
```
@Data
@AllArgsConstructor
@Builder
public class Country {
private String id;
private String name;
private String nativeName;
private int phoneCode;
private String continent;
private String capital;
private String currency;
private List languages;
}
```
```
Country country = Country.builder()
.id("TR")
.name("Turkey")
.build();
```