Skip to content

Commit

Permalink
Pattern Strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Feb 26, 2020
1 parent 1bf028d commit ec795fb
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/strategy/Annotation.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
##############################
# STRATEGY #
# STRATEGY #
##############################

- We use interfaces to use polymorphism in our doing.

- We broke the use of several 'ifs', with that we can invoke a method that according to its
instance it can call a method implemented differently than another instance.
16 changes: 16 additions & 0 deletions src/strategy/Budget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package strategy;

public class Budget {

private double value;

Budget(double value) {
super();
this.value = value;
}

public double getValue() {
return value;
}

}
17 changes: 16 additions & 1 deletion src/strategy/StrategyMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@
public class StrategyMain {

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

Budget budget = new Budget(100);

Tax icms = new TaxICMS();
Tax inss = new TaxINSS();

TaxCalculator taxCalculator = new TaxCalculator();

// Here we passes a Strategy for Calculator Tax, and in execution time, we will know what will be executed ICMS or INSS
taxCalculator.calculate(budget, icms);//10
taxCalculator.calculate(budget, inss);//20

// I can create any Tax and not broken my code, because I using pattern Strategy, using much Interfaces

System.out.println("--------------------------------------------- End Test Strategy");
}

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

public interface Tax {

public double calculate(Budget budget);

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

public class TaxCalculator {

public void calculate(Budget budget, Tax anyTax) {

double valueResult = anyTax.calculate(budget);
System.out.println("Value: " + valueResult + ". Class @Override: " + anyTax.getClass().getSimpleName());
}

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

public class TaxICMS implements Tax {

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

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

public class TaxINSS implements Tax {

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

}

0 comments on commit ec795fb

Please sign in to comment.