forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dragon.java
45 lines (39 loc) · 1.06 KB
/
Dragon.java
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
package com.iluwatar.facet.dragon;
import com.iluwatar.facet.Attack;
/**
* Dragon object needs to be protected, since the other objects shouldn't be
* allowed to edit its health. That is why it is in its own package with
* {@link DragonFacet} and all the functions have no access level modifiers,
* meaning only classes in the same package have access.
*/
public class Dragon {
private int health;
public Dragon(int health) {
this.health = health;
}
int facetedGetHealth() {
return health;
}
/**
* This has no access level modifier, so only other classes within
* the same package can access this function. This protects the knight
* from being able to arbitrarily change the dragon's health.
*
* @param health The new health of the dragon
*/
void setHealth(int health) {
this.health = health;
}
void facetedReceiveAttack(Attack attack) {
switch (attack) {
case ARROW:
health -= 10;
break;
case WATER_PISTOL:
health -= 15;
break;
default:
health -= 5;
}
}
}