Skip to content

Commit 06205d1

Browse files
authored
Add chain of responsibility design pattern (#56)
1 parent 6879f4d commit 06205d1

File tree

10 files changed

+132
-0
lines changed

10 files changed

+132
-0
lines changed

DesignPatterns/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ with practical examples and best practices for using design patterns to create r
99

1010
### Behavioral Design Patterns 💪
1111

12+
- [Chain of Responsibility](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/chain/of/responsibility) 🔗
1213
- [Command](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/command) 📝
1314
- [Execute Around Method (EAM)](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/eam)
1415
- [Observer](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/observer) 👀
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
enum Banknote {
4+
$10,
5+
$20,
6+
$50,
7+
$100,
8+
$200;
9+
10+
int getValue() {
11+
return Integer.parseInt(this.name().substring(1));
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
import java.util.EnumMap;
4+
import java.util.Map;
5+
6+
class BanknotePocket {
7+
8+
private final Map<Banknote, Integer> noteToAmount = new EnumMap<>(Banknote.class);
9+
10+
void add(Banknote note, Integer amount) {
11+
noteToAmount.put(note, amount);
12+
}
13+
14+
Integer get(Banknote note) {
15+
return noteToAmount.getOrDefault(note, 0);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
class Dollar100WithdrawalChain extends WithdrawalChain {
4+
5+
Dollar100WithdrawalChain(int amount) {
6+
super(Banknote.$100, amount);
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
class Dollar10WithdrawalChain extends WithdrawalChain {
4+
5+
Dollar10WithdrawalChain(int amount) {
6+
super(Banknote.$10, amount);
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
class Dollar200WithdrawalChain extends WithdrawalChain {
4+
5+
Dollar200WithdrawalChain(int amount) {
6+
super(Banknote.$200, amount);
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
class Dollar20WithdrawalChain extends WithdrawalChain {
4+
5+
Dollar20WithdrawalChain(int amount) {
6+
super(Banknote.$20, amount);
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
class Dollar50WithdrawalChain extends WithdrawalChain {
4+
5+
Dollar50WithdrawalChain(int amount) {
6+
super(Banknote.$50, amount);
7+
}
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
abstract class WithdrawalChain {
4+
5+
protected final Banknote banknote;
6+
protected int banknoteAmount;
7+
protected WithdrawalChain nextChain;
8+
9+
WithdrawalChain(Banknote banknote, int amount) {
10+
this.banknote = banknote;
11+
this.banknoteAmount = amount;
12+
}
13+
14+
BanknotePocket withdraw(int currency) {
15+
int banknoteValue = banknote.getValue();
16+
int restValue = currency;
17+
int banknoteCounter = 0;
18+
19+
while (restValue - banknoteValue >= 0 && banknoteAmount > 0) {
20+
restValue -= banknoteValue;
21+
banknoteCounter++;
22+
}
23+
24+
BanknotePocket pocket = nextChain == null
25+
? new BanknotePocket()
26+
: nextChain.withdraw(restValue);
27+
28+
pocket.add(banknote, banknoteCounter);
29+
return pocket;
30+
}
31+
32+
WithdrawalChain next(WithdrawalChain next) {
33+
nextChain = next;
34+
return next;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class WithdrawalChainTest {
7+
8+
@Test
9+
public void testWithdrawBanknotes() {
10+
WithdrawalChain parentChain = new Dollar200WithdrawalChain(2);
11+
parentChain.next(new Dollar100WithdrawalChain(2))
12+
.next(new Dollar50WithdrawalChain(2))
13+
.next(new Dollar20WithdrawalChain(2))
14+
.next(new Dollar10WithdrawalChain(2));
15+
16+
BanknotePocket pocket = parentChain.withdraw(360);
17+
18+
Assertions.assertEquals(1, pocket.get(Banknote.$200));
19+
Assertions.assertEquals(1, pocket.get(Banknote.$100));
20+
Assertions.assertEquals(1, pocket.get(Banknote.$50));
21+
Assertions.assertEquals(0, pocket.get(Banknote.$20));
22+
Assertions.assertEquals(1, pocket.get(Banknote.$10));
23+
}
24+
}

0 commit comments

Comments
 (0)