Skip to content

Commit

Permalink
Add Chain Of Responsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Feb 27, 2020
1 parent 4911dc5 commit 1080159
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

Project that implements some design patterns using Java 13.

- Strategy
- Strategy
- Chain Of Responsibility
28 changes: 28 additions & 0 deletions src/chainOfResponsibility/Annotation.txt
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.

30 changes: 30 additions & 0 deletions src/chainOfResponsibility/Budget.java
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.
}

}
24 changes: 24 additions & 0 deletions src/chainOfResponsibility/ChainOfResponsibilityMain.java
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");
}

}
9 changes: 9 additions & 0 deletions src/chainOfResponsibility/Discount.java
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);

}
17 changes: 17 additions & 0 deletions src/chainOfResponsibility/DiscountCalculator.java
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
}

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

}
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;
}

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

}
15 changes: 15 additions & 0 deletions src/chainOfResponsibility/NoDiscount.java
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
}

}
Binary file added src/chainOfResponsibility/print-for-help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/strategy/Annotation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@
# STRATEGY #
##############################





##### Language - EN

- 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.





##### Language - PT-BR

- Usamos interfaces para usar polimorfismo em nossas ações.

- Nós quebramos o uso de vários 'ses', com isso podemos invocar um método que, de acordo com sua
instância, ele pode chamar um método implementado de maneira diferente de outra instância.

0 comments on commit 1080159

Please sign in to comment.