-
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
12 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes
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,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. |
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,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. | ||
} | ||
|
||
} |
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 @@ | ||
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"); | ||
} | ||
|
||
} |
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,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); | ||
|
||
} |
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,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. | ||
} | ||
|
||
} |
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 @@ | ||
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; | ||
} | ||
|
||
} |
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,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; | ||
} | ||
|
||
} |
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,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)); | ||
} | ||
|
||
} |
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,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)); | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.