-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisitor.js
56 lines (46 loc) · 1.08 KB
/
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
// Visitor is a behavioral design pattern.
// Посетитель - поведенческий шаблон проектирования.
class Chocolate {
accept(visitor) {
visitor(this);
}
}
class ChocolateMilk extends Chocolate {
constructor() {
super();
}
info() {
console.log('Молочный шоколад!');
}
}
class ChocolateDark extends Chocolate {
constructor() {
super();
}
info() {
console.log('Тёмный шоколад!');
}
}
class ChocolateWithNuts extends Chocolate {
constructor() {
super();
}
info() {
console.log('Шоколад с орехами!');
}
}
function visitor(chocolate) {
if (
chocolate instanceof ChocolateMilk ||
chocolate instanceof ChocolateDark ||
chocolate instanceof ChocolateWithNuts
) {
chocolate.export = chocolate.info();
}
}
const myMilkChocolate = new ChocolateMilk();
const myDarkChocolate = new ChocolateDark();
const myChocolateWithNuts = new ChocolateWithNuts();
myMilkChocolate.accept(visitor);
myDarkChocolate.accept(visitor);
myChocolateWithNuts.accept(visitor);