Skip to content

Commit b5895ee

Browse files
committed
chain of responsibility added
1 parent 8799f17 commit b5895ee

File tree

12 files changed

+277
-0
lines changed

12 files changed

+277
-0
lines changed

chain-responsibility/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# chain-responsibility
2+
This is repository of http://androidcode.pl blog. It shows uses of Chain of responsibility pattern in Android. It is a part of Design Patterns - Chain of responsibility post in the blog.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//create default nominals scheme
5+
Nominal nominal100 = new ConcreteNominal(100);
6+
Nominal nominal50 = new ConcreteNominal(50);
7+
Nominal nominal20 = new ConcreteNominal(20);
8+
Nominal nominal10 = new ConcreteNominal(10)
9+
Nominal nominal5 = new ConcreteNominal(5);
10+
Nominal nominal2 = new ConcreteNominal(2);
11+
Nominal nominal1 = new ConcreteNominal(1);
12+
13+
//cashier gets client's transaction
14+
Exchange transaction1 = new Exchange(1500, Currency.PLN, Currency.USD);
15+
16+
//cashier defines default nominals scheme
17+
nominal100.setSuccessor(nominal50);
18+
nominal50.setSuccessor(nominal20);
19+
nominal20.setSuccessor(nominal10);
20+
nominal10.setSuccessor(nominal5);
21+
nominal5.setSuccessor(nominal2);
22+
nominal2.setSuccessor(nominal1);
23+
24+
//simulate transaction
25+
Exchange result1 = nominal100.calculate(transaction1);
26+
if(result.getLeftAmount() == 0) {
27+
//success - can withdraw
28+
}
29+
else {
30+
//fail - can not withdraw this transaction based on current nominals scheme
31+
}
32+
//show nominals to withdraw
33+
result.getOperations();
34+
//for course 3.67 the result is:
35+
//4x100, 1x5, 1x2, 1x1
36+
37+
//cashier gets new transaction
38+
Exchange transaction2 = new Exchange(5000, Currency.USD, Currency.GBP);
39+
//nominals GBP are the same like USD so no need to update the chain
40+
41+
//in deposit there is a lot of nominal 1
42+
//so cashier wants to spent nominal 1 before nominal 5 and 2
43+
nominal10.setSuccessor(nominal1);
44+
nominal1.setSuccessor(nominal2);
45+
nominal2.setSuccessor(nominal5);
46+
47+
//simulate transaction like above
48+
nominal100.calculate(transaction2);
49+
//for course 0.761 and 100x20, 50x30, 10x20 nominals available in deposit to withdraw the result is:
50+
//20x100, 30x50, 10x20, 10x10, 5x1
51+
}
52+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public enum Currency {
2+
3+
PLN(1),
4+
USD(2),
5+
GBP(3),
6+
EUR(4);
7+
//more currencies
8+
9+
private int currency;
10+
11+
public Currency(int currency) {
12+
this.currency = currency;
13+
}
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Exchange {
2+
3+
private int amount;
4+
private int leftAmount;
5+
private double cours;
6+
private String operations;
7+
private Currency start;
8+
private Currency target;
9+
10+
public Exchange(int amount, Currency start, Currency target) {
11+
this.amount = amount;
12+
this.start = start;
13+
this.target = target;
14+
//get cours from outside between currencies start and target
15+
leftAmount = amount * course;
16+
operations = "";
17+
}
18+
19+
public int getLeftAmount() {
20+
return leftAmount;
21+
}
22+
23+
public void setLeftAmount(int left) {
24+
this.leftAmount = left;
25+
}
26+
27+
public void addOperation(String operation) {
28+
operations += operation + "\n";
29+
}
30+
31+
public String getOperations() {
32+
return operations;
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class ConcreteNominal extends Nominal {
2+
3+
private final int NOMINAL;
4+
5+
public Nominal(int nominal) {
6+
this.NOMINAL = nominal;
7+
}
8+
9+
@Override
10+
protected void withdraw(Exchange transaction) {
11+
if(transaction.getLeftAmount() >= NOMINAL) {
12+
int count = transaction.getLeftAmount() / NOMINAL;
13+
int rest = transaction.getLeftAmount() % NOMINAL;
14+
transaction.setLeftAmount(rest);
15+
transaction.addOperation(count + " x Nominal " + NOMINAL);
16+
}
17+
}
18+
19+
@Override
20+
protected boolean canWithdraw(Exchange transaction) {
21+
if(transaction.getLeftAmount() >= NOMINAL && isDepositEnough(transaction))
22+
return true;
23+
else
24+
return false;
25+
}
26+
27+
private boolean isDepositEnough(Exchange transaction) {
28+
//check is concrete nominal enough in deposit
29+
return true; //mocked
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public abstract class Nominal {
2+
3+
protected Nominal successor;
4+
//some fields
5+
6+
protected abstract void withdraw(Exchange transaction);
7+
protected abstract boolean canWithdraw(Exchange transaction);
8+
9+
public void setSuccessor(Nominal successor) {
10+
this.successor = successor;
11+
}
12+
13+
public Exchange calculate(Exchange transaction) {
14+
if(canWithdraw(transaction))
15+
withdraw(transaction);
16+
if(transaction.getLeftAmount() > 0 && successor != null)
17+
return successor.calculate(transaction);
18+
else
19+
return transaction;
20+
}
21+
22+
//some other methods
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//create chain of responsibility depends
5+
Handler main = new ConcreteHandler1();
6+
Handler handler2 = new ConcreteHandler2();
7+
Handler handler3 = new ConcreteHandler3();
8+
main.setSuccessor(handler2);
9+
handler2.setSuccessor(handler3);
10+
11+
//carry out request to chain
12+
Request request1 = new Request("Content", Type.TYPE2);
13+
main.handle(request1); //handler2 will act
14+
15+
Request request2 = new Request("Content", Type.TYPE4);
16+
main.handle(request2); //no handler acts
17+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Request {
2+
3+
private String content;
4+
private Type type;
5+
//other fields
6+
7+
public Request(String content, Type type) {
8+
this.content = content;
9+
this.type = type;
10+
}
11+
12+
public String getContent() {
13+
return content;
14+
}
15+
16+
public Type getType() {
17+
return type;
18+
}
19+
20+
//other methods
21+
22+
public enum Type {
23+
24+
TYPE1(1),
25+
TYPE2(2),
26+
TYPE3(3),
27+
TYPE4(4),
28+
TYPE5(5);
29+
//other types
30+
31+
private int type;
32+
33+
public Type(int type) {
34+
this.type = type;
35+
}
36+
}
37+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ConcreteHandler1 extends Handler {
2+
3+
@Override
4+
protected void action(Request request) {
5+
//do specific action for ConcreteHandler1
6+
}
7+
8+
@Override
9+
protected boolean canExecute(Request request) {
10+
if(request.getType() == Type.TYPE1)
11+
return true;
12+
else
13+
return false;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ConcreteHandler2 extends Handler {
2+
3+
@Override
4+
protected void action(Request request) {
5+
//do specific action for ConcreteHandler2
6+
}
7+
8+
@Override
9+
protected boolean canExecute(Request request) {
10+
if(request.getType() == Type.TYPE2)
11+
return true;
12+
else
13+
return false;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ConcreteHandler3 extends Handler {
2+
3+
@Override
4+
protected void action(Request request) {
5+
//do specific action for ConcreteHandler3
6+
}
7+
8+
@Override
9+
protected boolean canExecute(Request request) {
10+
if(request.getType() == Type.TYPE3)
11+
return true;
12+
else
13+
return false;
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public abstract class Handler {
2+
3+
protected Handler successor;
4+
//some fields
5+
6+
protected abstract void action(Request request);
7+
protected abstract boolean canExecute(Request request);
8+
9+
public void setSuccessor(Handler successor) {
10+
this.successor = successor;
11+
}
12+
13+
public void handle(Request request) {
14+
if(canExecute(request))
15+
action(request);
16+
else if(successor != null)
17+
successor.handle(request);
18+
}
19+
20+
//some other methods
21+
}

0 commit comments

Comments
 (0)