Skip to content

Commit

Permalink
Add Observer
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Mar 18, 2020
1 parent d008f50 commit 289aaa3
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ Project that implements some design patterns using Java 13.

- Strategy
- Chain Of Responsibility
- Template Method
- Template Method
- Decorator
- State
- Builder
- Observer
7 changes: 7 additions & 0 deletions src/observer/ActionAfterGeneratingNote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package observer;

public interface ActionAfterGeneratingNote {

void execute(Invoice invoice);

}
21 changes: 21 additions & 0 deletions src/observer/Annotation.txt
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.
66 changes: 66 additions & 0 deletions src/observer/BuilderInvoice.java
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;
}


}
10 changes: 10 additions & 0 deletions src/observer/EmailSender.java
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...");
}

}
101 changes: 101 additions & 0 deletions src/observer/Invoice.java
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;
}


}
11 changes: 11 additions & 0 deletions src/observer/InvoiceDao.java
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...");

}

}
27 changes: 27 additions & 0 deletions src/observer/NoteItem.java
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;
}

}
34 changes: 34 additions & 0 deletions src/observer/ObserverMain.java
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");
}

}
10 changes: 10 additions & 0 deletions src/observer/Printer.java
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...");
}

}
11 changes: 11 additions & 0 deletions src/observer/SmsSender.java
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...");

}

}

0 comments on commit 289aaa3

Please sign in to comment.