-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChainOfResponsibility.java
More file actions
162 lines (129 loc) · 3.89 KB
/
ChainOfResponsibility.java
File metadata and controls
162 lines (129 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
abstract class Creature2 {
public abstract int getAttack();
public abstract int getDefense();
}
class Goblin extends Creature2 {
public Game2 game;
public RootModifier modifier;
public Goblin(Game2 game) {
this.game = game;
this.modifier = new RootModifier(this, game);
modifier.add(new GoblinKingAttackBuff(this, game));
modifier.add(new GoblinDefenceBuff(this, game));
}
private int getBaseAttack() {
return 1;
}
private int getBaseDefense() {
return 1;
}
@Override
public int getAttack() {
return modifier.handle(getBaseAttack(), Statistic.ATTACK);
}
@Override
public int getDefense() {
return modifier.handle(getBaseDefense(), Statistic.DEFENSE);
}
}
interface CreatureStatisticModifier {
void add(CreatureStatisticModifier next);
int handle(int value, Statistic attack);
}
class RootModifier implements CreatureStatisticModifier {
CreatureStatisticModifier next = null;
Creature2 creature;
Game2 game;
RootModifier(Creature2 creature, Game2 game) {
this.creature = creature;
this.game = game;
}
@Override
public void add(CreatureStatisticModifier newMod) {
if (this.next == null) {
this.next = newMod;
} else {
this.next.add(newMod);
}
}
@Override
public int handle(int value, Statistic stat) {
if (next != null) {
return next.handle(value, stat);
}
return value;
}
}
class GoblinKingAttackBuff extends RootModifier {
GoblinKingAttackBuff(Creature2 creature, Game2 game) {
super(creature, game);
}
@Override
public int handle(int value, Statistic stat) {
int newValue = value;
if (stat.equals(Statistic.ATTACK)) {
for(Creature2 c : this.game.creatures) {
if (c instanceof GoblinKing && c != this.creature) {
newValue++;
}
}
}
return super.handle(newValue, stat);
}
}
class GoblinDefenceBuff extends RootModifier {
GoblinDefenceBuff(Creature2 creature, Game2 game) {
super(creature, game);
}
@Override
public int handle(int value, Statistic stat) {
int newValue = value;
if (stat.equals(Statistic.DEFENSE)) {
for(Creature2 c : this.game.creatures) {
if (c instanceof Goblin && c != this.creature) {
newValue++;
}
}
}
return super.handle(newValue, stat);
}
}
class GoblinKing extends Goblin {
public GoblinKing(Game2 game) {
super(game);
}
@Override
public int getAttack() {
return 3;
}
@Override
public int getDefense() {
return 3;
}
}
enum Statistic {
ATTACK, DEFENSE
}
class Game2 {
public List<Creature2> creatures = new ArrayList<>();
}
public class ChainOfResponsibility {
public static void main(String[] args) {
Game2 game = new Game2();
Goblin goblin = new Goblin(game);
game.creatures.add(goblin);
assertEquals(1, goblin.getAttack());
assertEquals(1, goblin.getDefense());
game.creatures.add(new Goblin(game));
game.creatures.add(new Goblin(game));
assertEquals(1, goblin.getAttack());
assertEquals(3, goblin.getDefense());
game.creatures.add(new GoblinKing(game));
assertEquals(2, goblin.getAttack());
assertEquals(4, goblin.getDefense());
System.out.println("Success");
}
}