-
Notifications
You must be signed in to change notification settings - Fork 75
/
Visitor.js
100 lines (81 loc) · 2.28 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
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
'use strict';
class Visitor {
constructor() {
console.log('Visitor Class created!');
}
visitConcreteElementA(ConcreteElementA) {
console.log('Visitor.visitConcreteElementA invoked');
}
visitConcreteElementB(ConcreteElementB) {
console.log('Visitor.visitConcreteElementB invoked');
}
}
class ConcreteVisitor1 extends Visitor {
constructor() {
super();
console.log('ConcreteVisitor1 Class created!');
}
visitConcreteElementA(ConcreteElementA) {
console.log('ConcreteVisitor1.visitConcreteElementA invoked');
}
visitConcreteElementB(ConcreteElementB) {
console.log('ConcreteVisitor1.visitConcreteElementB invoked');
}
}
class ConcreteVisitor2 extends Visitor {
constructor() {
super();
console.log('ConcreteVisitor2 Class created!');
}
visitConcreteElementA(ConcreteElementA) {
console.log('ConcreteVisitor2.visitConcreteElementA invoked');
}
visitConcreteElementB(ConcreteElementB) {
console.log('ConcreteVisitor2.visitConcreteElementB invoked');
}
}
class ObjectStructure {
constructor() {
console.log('ObjectStructure Class created!');
}
}
class Element {
constructor() {
console.log('Element Class created!');
}
Accept(visitor) {
console.log('Element.visitConcreteElementB invoked');
}
}
class ConcreteElementA extends Element {
constructor() {
super();
console.log('ConcreteElementA Class created!');
}
accept(visitor) {
console.log('ConcreteElementA.accept invoked');
visitor.visitConcreteElementA(this);
}
operationA() {
console.log('ConcreteElementA.operationA invoked');
}
}
class ConcreteElementB extends Element {
constructor() {
super();
console.log('ConcreteElementB Class created!');
}
accept(visitor) {
console.log('ConcreteElementB.accept invoked');
visitor.visitConcreteElementB(this);
}
operationB() {
console.log('ConcreteElementB.operationB invoked');
}
}
let visitor1 = new ConcreteVisitor1();
let visitor2 = new ConcreteVisitor2();
let elementA = new ConcreteElementA();
let elementB = new ConcreteElementB();
elementA.accept(visitor1);
elementB.accept(visitor2);