-
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
11 changed files
with
303 additions
and
1 deletion.
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
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,7 @@ | ||
package observer; | ||
|
||
public interface ActionAfterGeneratingNote { | ||
|
||
void execute(Invoice invoice); | ||
|
||
} |
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,21 @@ | ||
############################### | ||
# Observer # | ||
############################### | ||
|
||
|
||
|
||
|
||
|
||
##### Language - EN | ||
|
||
|
||
- | ||
|
||
|
||
##### Language - PT-BR | ||
|
||
- Quando o acoplamento da nossa classe está crescendo, ou quando temos diversas ações | ||
diferentes a serem executadas após um determinado processo, podemos implementar o Observer. | ||
|
||
- Ele permite que diversas ações sejam executadas de forma transparente à classe principal, | ||
reduzindo o acoplamento entre essas ações, facilitando a manutenção e evolução do código. |
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,66 @@ | ||
package observer; | ||
|
||
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; | ||
|
||
private List<ActionAfterGeneratingNote> allActions; | ||
|
||
public BuilderInvoice() { | ||
this.allActions = new ArrayList<>(); | ||
} | ||
|
||
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 void addAction(ActionAfterGeneratingNote action) { | ||
this.allActions.add(action); | ||
} | ||
|
||
public Invoice builder() { | ||
Invoice invoice = new Invoice(companyName, cnpj, date, grossAmountl, taxes, allItems, notes); | ||
|
||
// Observer | ||
for (ActionAfterGeneratingNote actions : allActions) { | ||
actions.execute(invoice); | ||
} | ||
|
||
return invoice; | ||
} | ||
|
||
|
||
} |
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,10 @@ | ||
package observer; | ||
|
||
public class EmailSender implements ActionAfterGeneratingNote { | ||
|
||
@Override | ||
public void execute(Invoice invoice) { | ||
System.out.println("Send e-mail..."); | ||
} | ||
|
||
} |
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 observer; | ||
|
||
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,11 @@ | ||
package observer; | ||
|
||
public class InvoiceDao implements ActionAfterGeneratingNote { | ||
|
||
@Override | ||
public void execute(Invoice invoice) { | ||
System.out.println("Save invoice..."); | ||
|
||
} | ||
|
||
} |
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 observer; | ||
|
||
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; | ||
} | ||
|
||
} |
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,34 @@ | ||
package observer; | ||
|
||
public class ObserverMain { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("--------------------------------------------- Begin Test Observer"); | ||
|
||
|
||
BuilderInvoice builderInvoice = new BuilderInvoice(); | ||
|
||
builderInvoice.toCompany("Murillo Pezzuol LTDA") | ||
.withCnpj("987654321") | ||
.withItem(new NoteItem("Notebook", 100)) | ||
.withNotes("Anottation....") | ||
.onCurrentDate(); | ||
|
||
// Add to the list the actions that my observer will iterate | ||
builderInvoice.addAction(new EmailSender()); | ||
builderInvoice.addAction(new InvoiceDao()); | ||
builderInvoice.addAction(new SmsSender()); | ||
builderInvoice.addAction(new Printer()); | ||
|
||
Invoice invoice = builderInvoice.builder();//Return Object Invoice | ||
|
||
|
||
System.out.println("Gross Amountl: " + invoice.getGrossAmountl()); // 100 | ||
|
||
// OUT -> All registered actions and that our 'observer' has found and executed must be printed. | ||
|
||
|
||
System.out.println("--------------------------------------------- End Test Observer"); | ||
} | ||
|
||
} |
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,10 @@ | ||
package observer; | ||
|
||
public class Printer implements ActionAfterGeneratingNote { | ||
|
||
@Override | ||
public void execute(Invoice invoice) { | ||
System.out.println("Printer..."); | ||
} | ||
|
||
} |
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,11 @@ | ||
package observer; | ||
|
||
public class SmsSender implements ActionAfterGeneratingNote { | ||
|
||
@Override | ||
public void execute(Invoice invoice) { | ||
System.out.println("Send SMS..."); | ||
|
||
} | ||
|
||
} |