-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor.ts
45 lines (38 loc) · 1.14 KB
/
visitor.ts
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
(function () {
interface IComponent {
accept(visitor: IVisitor): void,
}
interface IVisitor {
visitComp1(component: ConcreteComponent1): void,
visitComp2(component: ConcreteComponent2): void,
}
class ConcreteComponent1 implements IComponent {
accept(visitor: IVisitor) {
visitor.visitComp1(this);
}
someOperationComp1() {
console.log('someOperationComp1');
}
}
class ConcreteComponent2 implements IComponent {
accept(visitor: IVisitor) {
visitor.visitComp2(this);
}
someOperationComp2() {
console.log('someOperationComp2');
}
}
class Visitor implements IVisitor {
visitComp1(component: ConcreteComponent1) {
component.someOperationComp1();
}
visitComp2(component: ConcreteComponent2) {
component.someOperationComp2();
}
}
const visitor = new Visitor();
const component1 = new ConcreteComponent1();
const component2 = new ConcreteComponent2();
component1.accept(visitor);
component2.accept(visitor);
})();