-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathPatternCraft - Visitor.js
57 lines (49 loc) · 1.48 KB
/
PatternCraft - Visitor.js
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
/*
Description:
The Visitor Design Pattern can be used, for example, to determine how an attack deals a different amount of damage to a unit in the StarCraft game.
The pattern consists of delegating the responsibility to a different class.
When a unit takes damage it can tell the visitor what to do with itself.
Your Task
Complete the code so that when a Tank attacks a Marine it takes 21 damage and when a Tank attacks a Marauder it takes 32 damage.
The Marine's initial health should be set to 100 and the Marauder's health should be set to 125.
You have 3 classes:
Marine: has a health property and accept(visitor) method
Marauder: has a health property and accept(visitor) method
TankBullet: the visitor class. Has visitLight(unit) and visitArmored(unit) methods
Resouces
PatternCraft > Visitor
SourceMaking > Visitor
Wikipedia > Visitor
PatternCraft series
State Pattern
Strategy Pattern
Visitor Pattern
Decorator Pattern
Adapter Pattern
Command Pattern
The original PatternCraft series (by John Lindquist) is a collection of Youtube videos that explains some of the design patterns and how they are used (or could be) on StarCraft.
*/
class Marine {
constructor() {
this.health=100;
}
accept(visitor) {
return visitor.visitLight(this)
}
}
class Marauder {
constructor() {
this.health=125;
}
accept(visitor) {
return visitor.visitArmored(this)
}
}
class TankBullet {
visitLight(unit) {
return unit.health-=21
}
visitArmored(unit) {
return unit.health-=32
}
}