-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
61 lines (48 loc) · 1.34 KB
/
Car.java
File metadata and controls
61 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package creational.builder;
public class Car {
// Required fields
private final String brand;
// Optional fields
private final String model;
private final Integer year;
private Car(Builder builder) {
this.brand = builder.brand;
this.model = builder.model;
this.year = builder.year;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", model='" + model + '\'' +
", year=" + year +
'}';
}
public static Builder builder(String brand) {
if (brand == null || brand.isEmpty()) {
throw new IllegalArgumentException("Brand is required");
}
return new Builder(brand);
}
public static class Builder {
// Required
private final String brand;
// Optional - with default values
private String model = "L90";
private Integer year = 1398;
private Builder(String brand) {
this.brand = brand;
}
public Builder model(String model) {
this.model = model;
return this;
}
public Builder year(Integer year) {
this.year = year;
return this;
}
public Car build() {
return new Car(this);
}
}
}