-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
############################### | ||
# Builder # | ||
############################### | ||
|
||
|
||
|
||
|
||
|
||
##### Language - EN | ||
|
||
|
||
- The Builder makes it possible to separate the complexity of creating this object into a specific class for that, | ||
in addition to making it possible to implement optional attributes more easily. | ||
|
||
- Whenever we have a complex object to be created, which has several attributes, | ||
or that has complicated creation logic, we can hide it all in a Builder. | ||
|
||
- In addition to centralizing the creation code and facilitating maintenance, we also make life easier for the classes | ||
that need to create this complex class, after all the Builder interface tends to be clearer and easier to use. | ||
|
||
|
||
##### Language - PT-BR | ||
|
||
- O Builder possibilita a separação da complexidade da criação desse objeto em uma classe específica para isso, | ||
além de possibilitar a implementação de atributos opcionais mais facilmente. | ||
|
||
- Sempre que tivermos um objeto complexo de ser criado, que possui diversos atributos, | ||
ou que possui uma lógica de criação complicada, podemos esconder tudo isso em um Builder. | ||
|
||
- Além de centralizar o código de criação e facilitar a manutenção, ainda facilitamos a vida das classes | ||
que precisam criar essa classe complexa, afinal a interface do Builder tende a ser mais clara e fácil de ser usada. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package builder; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BuilderInvoice { | ||
|
||
private String companyName; | ||
private String cnpj; | ||
private List<NoteItem> allItems = new ArrayList<>(); | ||
private double grossAmountl; | ||
private double taxes; | ||
private String notes; | ||
private LocalDate date; | ||
|
||
public BuilderInvoice toCompany(String companyName) { | ||
this.companyName = companyName; | ||
return this; //Return this use for method in cascade for BUILDER | ||
} | ||
|
||
public BuilderInvoice withCnpj(String cnpj) { | ||
this.cnpj = cnpj; | ||
return this; //Return this use for method in cascade for BUILDER | ||
} | ||
|
||
public BuilderInvoice withItem(NoteItem item) { | ||
this.allItems.add(item); | ||
this.grossAmountl += item.getValue(); | ||
this.taxes = item.getValue() * 0.05; | ||
return this; //Return this use for method in cascade for BUILDER | ||
} | ||
|
||
public BuilderInvoice withNotes(String notes) { | ||
this.notes = notes; | ||
return this; //Return this use for method in cascade for BUILDER | ||
} | ||
|
||
public BuilderInvoice onCurrentDate() { | ||
this.date = LocalDate.now(); | ||
return this; //Return this use for method in cascade for BUILDER | ||
} | ||
|
||
public Invoice builder() { | ||
return new Invoice(companyName, cnpj, date, grossAmountl, taxes, allItems, notes); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package builder; | ||
|
||
public class BuilderMain { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("--------------------------------------------- Begin Test Builder"); | ||
|
||
|
||
// BUILDER -> Call in cascade because | ||
BuilderInvoice builderInvoice = new BuilderInvoice(); | ||
builderInvoice.toCompany("Murillo Pezzuol LTDA") | ||
.withCnpj("123456789") | ||
.withItem(new NoteItem("Notebook", 200)) | ||
.withItem(new NoteItem("Mouse", 300)) | ||
.withItem(new NoteItem("Keyboard", 400)) | ||
.withNotes("Anottation....") | ||
.onCurrentDate(); | ||
// continue if you want..... | ||
|
||
Invoice invoice = builderInvoice.builder();//Return Object Invoice | ||
|
||
System.out.println("Company Name: " + invoice.getCompanyName());// Murillo Pezzuol LTDA | ||
System.out.println("Gross Amountl: " + invoice.getGrossAmountl()); // 900 | ||
System.out.println("Taxes: " + invoice.getTaxes()); // 20 | ||
|
||
|
||
System.out.println("--------------------------------------------- End Test Builder"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package builder; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Invoice { | ||
|
||
private String companyName; | ||
private String cnpj; | ||
private LocalDate dateOfIssue; | ||
private double grossAmountl; | ||
private double taxes; | ||
private List<NoteItem> allItems = new ArrayList<>(); | ||
private String notes; | ||
|
||
|
||
public Invoice(String companyName, String cnpj, LocalDate dateOfIssue, double grossAmountl, double taxes, | ||
List<NoteItem> allItems, String notes) { | ||
super(); | ||
this.companyName = companyName; | ||
this.cnpj = cnpj; | ||
this.dateOfIssue = dateOfIssue; | ||
this.grossAmountl = grossAmountl; | ||
this.taxes = taxes; | ||
this.allItems = allItems; | ||
this.notes = notes; | ||
} | ||
|
||
|
||
public String getCompanyName() { | ||
return companyName; | ||
} | ||
|
||
|
||
public void setCompanyName(String companyName) { | ||
this.companyName = companyName; | ||
} | ||
|
||
|
||
public String getCnpj() { | ||
return cnpj; | ||
} | ||
|
||
|
||
public void setCnpj(String cnpj) { | ||
this.cnpj = cnpj; | ||
} | ||
|
||
|
||
public LocalDate getDateOfIssue() { | ||
return dateOfIssue; | ||
} | ||
|
||
|
||
public void setDateOfIssue(LocalDate dateOfIssue) { | ||
this.dateOfIssue = dateOfIssue; | ||
} | ||
|
||
|
||
public double getGrossAmountl() { | ||
return grossAmountl; | ||
} | ||
|
||
|
||
public void setGrossAmountl(double grossAmountl) { | ||
this.grossAmountl = grossAmountl; | ||
} | ||
|
||
|
||
public double getTaxes() { | ||
return taxes; | ||
} | ||
|
||
|
||
public void setTaxes(double taxes) { | ||
this.taxes = taxes; | ||
} | ||
|
||
|
||
public List<NoteItem> getAllItems() { | ||
return allItems; | ||
} | ||
|
||
|
||
public void setAllItems(List<NoteItem> allItems) { | ||
this.allItems = allItems; | ||
} | ||
|
||
|
||
public String getNotes() { | ||
return notes; | ||
} | ||
|
||
|
||
public void setNotes(String notes) { | ||
this.notes = notes; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package builder; | ||
|
||
public class NoteItem { | ||
|
||
private String name; | ||
private double value; | ||
|
||
public NoteItem(String name, double value) { | ||
super(); | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public double getValue() { | ||
return value; | ||
} | ||
public void setValue(double value) { | ||
this.value = value; | ||
} | ||
|
||
} |