forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavioral_visitor.js
More file actions
67 lines (54 loc) · 1.61 KB
/
behavioral_visitor.js
File metadata and controls
67 lines (54 loc) · 1.61 KB
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
const VISITOR = {
id: 'visitor',
name: 'Visitor',
type: 'behavioral',
hint: 'Defines a new operation to a class without change',
definition: `Represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the elements on which it operates.`,
when: `an object structure includes many classes and you want to perform an operations
on the elements of that structure that depend on their classes`,
codeES5: `function bonusPattern(employee) {
if (employee instanceof Manager) employee.bonus = employee.salary * 2;
if (employee instanceof Developer) employee.bonus = employee.salary;
}
function Employee() {
this.bonus = 0;
}
Employee.prototype.accept = function(item) {
item(this);
};
function Manager(salary) {
this.salary = salary;
}
Manager.prototype = Object.create(Employee.prototype);
function Developer(salary) {
this.salary = salary;
}
Developer.prototype = Object.create(Employee.prototype);
module.exports = [Developer, Manager, bonusPattern];`,
codeES6: `function bonusPattern(employee) {
if (employee instanceof Manager) employee.bonus = employee.salary * 2;
if (employee instanceof Developer) employee.bonus = employee.salary;
}
class Employee {
constructor(salary) {
this.bonus = 0;
this.salary = salary;
}
accept(item) {
item(this);
}
}
class Manager extends Employee {
constructor(salary) {
super(salary);
}
}
class Developer extends Employee {
constructor(salary) {
super(salary);
}
}
export { Developer, Manager, bonusPattern };`
};
export default VISITOR;