-
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
206 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
Project that implements some design patterns using Java 13. | ||
|
||
- Strategy | ||
- Strategy | ||
- Chain Of Responsibility |
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 @@ | ||
############################### | ||
# Chain Of Responsibility # | ||
############################### | ||
|
||
|
||
|
||
|
||
|
||
##### Language - EN | ||
|
||
- The Chain of Responsibility standard fits like a glove when we have a list of commands to be executed according to a specific scenario, | ||
and we also know the next scenario that should be validated, if the previous one does not satisfy the condition. | ||
|
||
- In these cases, the Chain of Responsibility allows us to separate responsibilities into small and lean classes, | ||
and it also provides a flexible and uncoupled way to put these behaviors back together. | ||
|
||
|
||
|
||
|
||
|
||
##### Language - PT-BR | ||
|
||
- O padrão Chain of Responsibility cai como uma luva quando temos uma lista de comandos a serem executados de acordo com algum cenário em específico, | ||
e sabemos também qual o próximo cenário que deve ser validado, caso o anterior não satisfaça a condição. | ||
|
||
- Nesses casos, o Chain of Responsibility nos possibilita a separação de responsabilidades em classes pequenas e enxutas, | ||
e ainda provê uma maneira flexível e desacoplada de juntar esses comportamentos novamente. | ||
|
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,30 @@ | ||
package chainOfResponsibility; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
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,24 @@ | ||
package chainOfResponsibility; | ||
|
||
public class ChainOfResponsibilityMain { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("--------------------------------------------- Begin Test Chain Of Responsibility"); | ||
|
||
|
||
DiscountCalculator discountCalculator = new DiscountCalculator(); | ||
|
||
Budget budget = new Budget(500.1); | ||
budget.addItem(new Item("Mouse", 250.0)); | ||
budget.addItem(new Item("Keyboard", 320.1)); | ||
|
||
// The calculation will call the method in order as prescribed, until you find the discount applicable. In this case, discount on the budget with more than 500 cash. | ||
double discountFinal = discountCalculator.calculate(budget); | ||
|
||
System.out.println("Discount Final: " + discountFinal + "%"); //35.007000000000005% | ||
|
||
|
||
System.out.println("--------------------------------------------- End Test Chain Of Responsibility"); | ||
} | ||
|
||
} |
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,9 @@ | ||
package chainOfResponsibility; | ||
|
||
public interface Discount { | ||
|
||
double discount(Budget budget); | ||
|
||
void setNextDiscount(Discount nextDiscount); | ||
|
||
} |
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 chainOfResponsibility; | ||
|
||
public class DiscountCalculator { | ||
|
||
public double calculate(Budget budget) { | ||
Discount d1 = new DiscountForFiveItems(); | ||
Discount d2 = new DiscountForMoreThanFiveHundredMoneys(); | ||
Discount dEnd = new NoDiscount(); //we need a guy to take care of the discount if he goes through all the discounts, that is, do not apply the discount. | ||
|
||
// Add Discounts Available | ||
d1.setNextDiscount(d2); // If it is not the 'd1' discount, it calls the next discount which will be the 'd2' | ||
d2.setNextDiscount(dEnd); // If it is not the 'd2' discount, it calls the next discount which will be the 'dEnd', which is the last discount, which actually does not make any discount | ||
|
||
return d1.discount(budget); // chained call, so it will return the corresponding discount amount | ||
} | ||
|
||
} |
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 chainOfResponsibility; | ||
|
||
public class DiscountForFiveItems implements Discount { | ||
|
||
private Discount nextDiscount; | ||
|
||
@Override | ||
public double discount(Budget budget) { | ||
if (budget.getItems().size() > 5) { | ||
return budget.getValue() * 0.01; // Discount 1% | ||
} else { | ||
return nextDiscount.discount(budget); // Else, call next discount | ||
} | ||
} | ||
|
||
@Override | ||
public void setNextDiscount(Discount nextDiscount) { | ||
this.nextDiscount = nextDiscount; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/chainOfResponsibility/DiscountForMoreThanFiveHundredMoneys.java
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 chainOfResponsibility; | ||
|
||
public class DiscountForMoreThanFiveHundredMoneys implements Discount { | ||
|
||
private Discount nextDiscount; | ||
|
||
@Override | ||
public double discount(Budget budget) { | ||
if (budget.getValue() > 500) { | ||
return budget.getValue() * 0.07; // Discount 7% | ||
} else { | ||
return nextDiscount.discount(budget); // Else, call next discount | ||
} | ||
} | ||
|
||
@Override | ||
public void setNextDiscount(Discount nextDiscount) { | ||
this.nextDiscount = nextDiscount; | ||
} | ||
|
||
} |
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 chainOfResponsibility; | ||
|
||
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,15 @@ | ||
package chainOfResponsibility; | ||
|
||
public class NoDiscount implements Discount { | ||
|
||
@Override | ||
public double discount(Budget budget) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void setNextDiscount(Discount nextDiscount) { | ||
//there is no next | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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