🏗️ Builder

🧱 Temel Kullanım

⭐ Kullanım Örneği

1
NewsAPIOptions options = NewsAPIOptions.Builder()
2
.setCountry("turkey")
3
.setCategory("sports")
4
.build();
Copied!

👨‍💻 Kod Örneği

1
public class NewsAPIOptions {
2
3
private String country;
4
private String category;
5
private String sources;
6
private String query;
7
8
private int pageSize;
9
private int page;
10
11
private NewsAPIOptions(Builder builder) {
12
this.country = builder.country;
13
this.category = builder.category;
14
this.sources = builder.sources;
15
this.query = builder.query;
16
this.pageSize = builder.pageSize;
17
this.page = builder.page;
18
}
19
20
public static Builder Builder() {
21
return new Builder();
22
}
23
24
public static final class Builder {
25
26
String country = "tr";
27
String category;
28
String sources;
29
String query;
30
31
int pageSize = -1;
32
int page = -1;
33
34
public Builder setCountry(String country) {
35
this.country = country;
36
return this;
37
}
38
39
public Builder setCategory(String category) {
40
this.category = category;
41
return this;
42
}
43
44
public Builder setSources(String sources) {
45
this.sources = sources;
46
return this;
47
}
48
49
public Builder setQuery(String query) {
50
this.query = query;
51
return this;
52
}
53
54
public Builder setPageSize(int pageSize) {
55
this.pageSize = pageSize;
56
return this;
57
}
58
59
public Builder setPage(int page) {
60
this.page = page;
61
return this;
62
}
63
64
public NewsAPIOptions build() {
65
return new NewsAPIOptions(this);
66
}
67
}
68
}
Copied!

🌟 Enum ile Tanımlama

⭐ Kullanım Örneği

1
NewsAPIOptions options = NewsAPIOptions.Builder()
2
.setCountry(NewsAPIOptions.Country.US)
3
.setCategory(NewsAPIOptions.Category.BUSINESS)
4
.build();
Copied!

👨‍💻 Kod Değişikliği

1
public class NewsAPIOptions {
2
3
...
4
5
String category;
6
String country;
7
8
...
9
10
public enum Category {
11
12
BUSINESS, ENTERTAINMENT, GENERAL, HEALTH, SCIENCE, SPORTS, TECHNOLOGY;
13
14
public String getValue() {
15
return this.name().toLowerCase();
16
}
17
}
18
19
public enum Country {
20
21
AE, AR, AT, AU, BE, BG, BR, CA, CH, CN, CO, CU, CZ, DE, EG, FR,
22
GB, GR, HK, HU, ID, IE, IL, IN, IT, JP, KR, LT, LV, MA, MX, MY,
23
NG, NL, NO, NZ, PH, PL, PT, RO, RS, RU, SA, SE, SG, SI, SK, TH,
24
TR, TW, UA, US, VE, ZA;
25
26
public String getValue() {
27
return this.name().toLowerCase();
28
}
29
}
30
31
public static final class Builder {
32
...
33
34
String category;
35
String country;
36
37
...
38
39
public Builder setCategory(Category category) {
40
this.category = category.getValue();
41
return this;
42
}
43
44
public Builder setCountry(Country country) {
45
this.country = country.getValue();
46
return this;
47
}
48
49
...
50
}
51
52
}
Copied!

🔗 Faydalı Kaynaklar

Last modified 1yr ago