Skip to content

Commit 2fc40e6

Browse files
committed
Merge branch '1-builder' of https://github.com/LBeghini/Java-Design-Patterns into 1-builder
2 parents 69ebbd5 + c780a5e commit 2fc40e6

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,35 @@ For example, let's say we want to create a crew member of a spaceship. It's a ro
1515
- birthDate
1616
- bloodType
1717

18+
### Without Builder
19+
1820
It would be annoying and difficult to understand having all these attributes in a single constructor:
1921

2022
```java
2123

22-
public CrewMember(Integer id, String name, Species specie, Rank rank, String serialNumber, Status status, Date birthDate, BloodType bloodType)
24+
public CrewMember(Integer id, String name, Specie specie, Rank rank, String serialNumber, Status status, Date birthDate, BloodType bloodType);
25+
26+
```
27+
28+
and the creation would be:
29+
```java
30+
31+
CrewMember crewMember = new CrewMember(1, "Kirk", Specie.HUMAN, Rank.CAPTAIN, "SC937-0176CEC", Status.DECEASED, LocalDate.of(2233, 3, 22), BloodType.AB);
32+
33+
```
34+
35+
witch is hard to understand what each part is, and is important to keep everything in the same place implemented by the constructor.
36+
37+
38+
### With Builder
39+
Creating a complex object gets easier by dividing the process in parts
40+
41+
```java
42+
43+
CrewMember crewMember = crewMemberBuilder.setName("Spok").setspecie(Specie.VULCANO).setRank(Rank.COMMANDER)
44+
.setSerialNumber("S 179-276 SP").setStatus(Status.MISSING)
45+
.setBirthDate(LocalDate.of(2285, 1, 6)).setBloodType(BloodType.T).build();
2346

2447
```
2548

26-
With Builder, creating a complex object gets easier by dividing the process in parts.
49+
witch is better to read and understand, and the order of the steps doesn't matter.

0 commit comments

Comments
 (0)