-
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
9 changed files
with
191 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,28 @@ | ||
############################## | ||
# Template Method # | ||
############################## | ||
|
||
|
||
|
||
|
||
|
||
##### Language - EN | ||
|
||
- When we have different algorithms that have similar structures, the Template Method is a good solution. | ||
With it, we were able to define, on a more macro level, the structure of the algorithm and leave "holes", which will be | ||
implemented differently by each of the specific implementations. | ||
|
||
- Thus, we reuse instead of repeating code, and facilitate possible evolutions, both of the algorithm in | ||
its macro structure, as well as the details of the algorithm, since each class has its own separate responsibility. | ||
|
||
|
||
|
||
|
||
##### Language - PT-BR | ||
|
||
- Quando temos diferentes algoritmos que possuem estruturas parecidas, o Template Method é uma boa solução. | ||
Com ele, conseguimos definir, em um nível mais macro, a estrutura do algoritmo e deixar "buracos", que serão | ||
implementados de maneira diferente por cada uma das implementações específicas. | ||
|
||
- Dessa forma, reutilizamos ao invés de repetirmos código, e facilitamos possíveis evoluções, tanto do algoritmo em | ||
sua estrutura macro, quanto dos detalhes do algoritmo, já que cada classe tem sua responsabilidade bem separada. |
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 templateMethod; | ||
|
||
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,23 @@ | ||
package templateMethod; | ||
|
||
public abstract class ConditionalTaxTemplate implements Tax{ | ||
|
||
@Override | ||
public double calculate(Budget budget) { | ||
|
||
if(mustUseMaxTaxAplly(budget)) { | ||
return maxTaxAplly(budget); // Aplly Max Tax | ||
} else { | ||
return minTaxAplly(budget); // Aplly Min Tax | ||
} | ||
|
||
} | ||
|
||
// 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,21 @@ | ||
package templateMethod; | ||
|
||
public class Item { | ||
|
||
private final String name; | ||
private final double value; | ||
|
||
public Item(String name, double value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public double getValue() { | ||
return 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,7 @@ | ||
package templateMethod; | ||
|
||
public interface Tax { | ||
|
||
public double calculate(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,31 @@ | ||
package templateMethod; | ||
|
||
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 templateMethod; | ||
|
||
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,27 @@ | ||
package templateMethod; | ||
|
||
import templateMethod.Item; | ||
|
||
public class TemplateMethodMain { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("--------------------------------------------- Begin Test Template Method"); | ||
|
||
|
||
Budget budget = new Budget(501); | ||
budget.addItem(new Item("Mouse", 90.0)); | ||
budget.addItem(new Item("Keyboard", 231.1)); | ||
budget.addItem(new Item("TV", 600.0)); | ||
|
||
ConditionalTaxTemplate conditionalTaxTemplateABCD = new TaxABCD(); //75.14999999999999 | ||
ConditionalTaxTemplate conditionalTaxTemplateEFGH = new TaxEFGH(); //50.1 | ||
|
||
// according to its implementation it will call the corresponding calculation, using TEMPLATE METHOD | ||
System.out.println("Calculate: " + conditionalTaxTemplateABCD.calculate(budget) + "%"); | ||
System.out.println("Calculate: " + conditionalTaxTemplateEFGH.calculate(budget) + "%"); | ||
|
||
|
||
System.out.println("--------------------------------------------- End Test Template Method"); | ||
} | ||
|
||
} |