Skip to content

Commit

Permalink
Add Decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Mar 3, 2020
1 parent d489a57 commit fa84a73
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/decorator/Annotation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
##############################
# Decorator #
##############################





##### Language - EN

- In the code example, we want a tax to have another tax in it, that is, a compound tax, that way it would be simple using decorator.

- We decorate tax A with tax B, decorate tax B with tax C, and so on.

- Whenever we realize that we have behaviors that can be composed of behaviors from other classes involved in the same hierarchy,
as was the case with taxes, which may consist of other taxes. Decorator introduces flexibility in the composition of these behaviors,
just choose at the moment of instantiation, which instances will be used to perform the work.


##### Language - PT-BR

- No exemplo do código, nós queremos que um imposto possa ter outro imposto nele, ou seja, imposto composto, dessa forma ficaria simples usando decorator.

- Decoramos um imposto A com o imposto B, decoramos imposto B com um imposto C, e assim por diante.

- Sempre que percebemos que temos comportamentos que podem ser compostos por comportamentos de outras classes envolvidas em uma mesma hierarquia,
como foi o caso dos impostos, que podem ser composto por outros impostos. O Decorator introduz a flexibilidade na composição desses comportamentos,
bastando escolher no momento da instanciação, quais instancias serão utilizadas para realizar o trabalho.
32 changes: 32 additions & 0 deletions src/decorator/Budget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package decorator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import templateMethod.Item;

public class Budget {

private double value;
private final List<Item> items;

public Budget(double value) {
super();
this.value = value;
items = new ArrayList<Item>();
}

public double getValue() {
return value;
}

public void addItem(Item item) {
items.add(item);
}

public List<Item> getItems() {
return Collections.unmodifiableList(items);// unmodifiableList -> Return a List immutable.
}

}
21 changes: 21 additions & 0 deletions src/decorator/ComplexTaxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package decorator;

public class ComplexTaxTest {

public static void main(String[] args) {
System.out.println("--------------------------------------------- Begin Test for Complex Tax");


Budget budget = new Budget(500);


// Tax in % = 0.2 + 0.1 + 0.06 (INSS + ICMS + ABCD)
// Calculate: 500 * 0.36 = 180%
Tax taxValue = new TaxINSS(new TaxICMS(new TaxABCD()));
System.out.println("Compound Tax Value: " + taxValue.calculate(budget) + "%");//180.0%


System.out.println("--------------------------------------------- End Test for Complex Tax");
}

}
23 changes: 23 additions & 0 deletions src/decorator/ConditionalTaxTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package decorator;

public abstract class ConditionalTaxTemplate extends Tax{

@Override
public double calculate(Budget budget) {

if(mustUseMaxTaxAplly(budget)) {
return maxTaxAplly(budget) + calculateOtherTax(budget); // Aplly Max Tax + calculateOtherTax
} else {
return minTaxAplly(budget) + calculateOtherTax(budget); // Aplly Min Tax + calculateOtherTax
}

}

// Protected for only children to see
protected abstract boolean mustUseMaxTaxAplly(Budget budget);

protected abstract double maxTaxAplly(Budget budget);

protected abstract double minTaxAplly(Budget budget);

}
26 changes: 26 additions & 0 deletions src/decorator/Tax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package decorator;

public abstract class Tax {

protected Tax otherTax;

// Constructor for otherTax for compound tax
public Tax(Tax otherTax) {
this.otherTax = otherTax;
}

public Tax() {

}

public abstract double calculate(Budget budget);

protected double calculateOtherTax(Budget budget) {
// Tax that is not compounded must return the value 0
if (this.otherTax == null)
return 0.0d;
else
return otherTax.calculate(budget); //Calculate other tax, always compound tax.
}

}
31 changes: 31 additions & 0 deletions src/decorator/TaxABCD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package decorator;

public class TaxABCD extends ConditionalTaxTemplate {

// If item in the list is greater than 100 it returns true
private boolean thereIsAnItemGreaterThan100InTheBudget(Budget budget) {
return budget.getItems().stream()
.anyMatch(i -> {
if (i.getValue() > 100) {
return true;
}
return false;
});
}

@Override
protected boolean mustUseMaxTaxAplly(Budget budget) {
return budget.getValue() > 500 && thereIsAnItemGreaterThan100InTheBudget(budget);
}

@Override
protected double maxTaxAplly(Budget budget) {
return budget.getValue() * 0.15;
}

@Override
protected double minTaxAplly(Budget budget) {
return budget.getValue() * 0.06;
}

}
20 changes: 20 additions & 0 deletions src/decorator/TaxEFGH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package decorator;

public class TaxEFGH extends ConditionalTaxTemplate {

@Override
protected boolean mustUseMaxTaxAplly(Budget budget) {
return budget.getValue() > 500;
}

@Override
protected double maxTaxAplly(Budget budget) {
return budget.getValue() * 0.10;
}

@Override
protected double minTaxAplly(Budget budget) {
return budget.getValue() * 0.06;
}

}
17 changes: 17 additions & 0 deletions src/decorator/TaxICMS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package decorator;

public class TaxICMS extends Tax {

public TaxICMS (Tax otherTax) {
super(otherTax);
}

public TaxICMS () {
}

@Override
public double calculate(Budget budget) {
return (budget.getValue() * 0.1) + (calculateOtherTax(budget));
}

}
18 changes: 18 additions & 0 deletions src/decorator/TaxINSS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package decorator;

public class TaxINSS extends Tax {

public TaxINSS(Tax otherTax) {
super(otherTax);
}

public TaxINSS() {
super();
}

@Override
public double calculate(Budget budget) {
return (budget.getValue() * 0.2) + (calculateOtherTax(budget));
}

}
Binary file added src/decorator/print-for-help-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/decorator/print-for-help-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fa84a73

Please sign in to comment.