Skip to content

Commit

Permalink
Add Template Method
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Feb 27, 2020
1 parent 1080159 commit d489a57
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
Project that implements some design patterns using Java 13.

- Strategy
- Chain Of Responsibility
- Chain Of Responsibility
- Template Method
28 changes: 28 additions & 0 deletions src/templateMethod/Annotation.txt
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.
32 changes: 32 additions & 0 deletions src/templateMethod/Budget.java
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.
}

}
23 changes: 23 additions & 0 deletions src/templateMethod/ConditionalTaxTemplate.java
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);

}
21 changes: 21 additions & 0 deletions src/templateMethod/Item.java
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;
}

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

public interface Tax {

public double calculate(Budget budget);

}
31 changes: 31 additions & 0 deletions src/templateMethod/TaxABCD.java
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;
}

}
20 changes: 20 additions & 0 deletions src/templateMethod/TaxEFGH.java
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;
}

}
27 changes: 27 additions & 0 deletions src/templateMethod/TemplateMethodMain.java
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");
}

}

0 comments on commit d489a57

Please sign in to comment.