An open API service indexing awesome lists of open source software.

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

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();
```