Skip to content

Commit 010986f

Browse files
committed
Improve examples
1 parent 0a0a7c2 commit 010986f

2 files changed

Lines changed: 50 additions & 32 deletions

File tree

‎JavaScript/1-theory.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
// Elements to be visited
4+
35
class Product {
46
constructor(name, price) {
57
this.name = name;
@@ -33,6 +35,8 @@ class Service {
3335
}
3436
}
3537

38+
// Visitors to be acceped
39+
3640
class Purchase {
3741
constructor(items, delivery) {
3842
this.items = [];
@@ -67,7 +71,7 @@ class Purchase {
6771

6872
class Inspection {
6973
constructor(items) {
70-
this.items = items;
74+
this.items = [...items];
7175
}
7276

7377
check() {

‎JavaScript/3-js-way.js‎

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
'use strict';
22

3+
// Elements to be visited
4+
35
class Product {
46
constructor(name, price) {
57
this.name = name;
68
this.price = price;
79
}
810

9-
isAvailable(conditions, callback) {
10-
callback(true); // Just a stub
11+
accept(visitor) {
12+
visitor.visit(this, this.inStock());
13+
}
14+
15+
inStock() {
16+
return true; // Just a stub
1117
}
1218
}
1319

@@ -19,35 +25,39 @@ class Service {
1925
this.name = name;
2026
}
2127

22-
isAvailable(conditions, callback) {
23-
const day = conditions.date.getDay();
24-
callback(day > Service.SUNDAY && day < Service.SATURDAY);
28+
accept(visitor) {
29+
const now = new Date();
30+
visitor.visit(this, this.isAvailableAt(now));
31+
}
32+
33+
isAvailableAt(date) {
34+
const day = date.getDay();
35+
return day > Service.SUNDAY && day < Service.SATURDAY;
2536
}
2637
}
2738

39+
// Visitors to be acceped
40+
2841
class Purchase {
2942
constructor(items, delivery) {
3043
this.items = [];
3144
this.delivery = null;
32-
for (const product of items) {
33-
product.isAvailable({}, (available) => {
34-
if (available) {
35-
console.log(`Product "${product.name}" is in stock`);
36-
this.items.push(product);
37-
} else {
38-
console.log(`Product "${product.name}" is out of stock`);
39-
}
40-
});
45+
for (const item of items) {
46+
item.accept(this);
47+
}
48+
delivery.accept(this);
49+
}
50+
51+
visit(item, available) {
52+
const proto = Object.getPrototypeOf(item);
53+
const className = proto.constructor.name;
54+
if (available) {
55+
console.log(`${className} "${item.name}" is available`);
56+
if (item instanceof Service) this.delivery = item;
57+
else this.items.push(item);
58+
} else {
59+
console.log(`${className} "${item.name}" is not available`);
4160
}
42-
const now = new Date();
43-
delivery.isAvailable({ date: now }, (available) => {
44-
if (available) {
45-
console.log(`Service "${delivery.name}" is available`);
46-
this.delivery = delivery;
47-
} else {
48-
console.log(`Service "${delivery.name}" is not available`);
49-
}
50-
});
5161
}
5262
}
5363

@@ -57,14 +67,18 @@ class Inspection {
5767
}
5868

5969
check() {
60-
for (const product of this.items) {
61-
product.isAvailable({}, (available) => {
62-
if (available) {
63-
console.log(`Product "${product.name}" is in stock`);
64-
} else {
65-
console.log(`Product "${product.name}" is out of stock`);
66-
}
67-
});
70+
for (const item of this.items) {
71+
item.accept(this);
72+
}
73+
}
74+
75+
visit(item, available) {
76+
const proto = Object.getPrototypeOf(item);
77+
const className = proto.constructor.name;
78+
if (available) {
79+
console.log(`${className} "${item.name}" is in stock`);
80+
} else {
81+
console.log(`${className} "${item.name}" is out of stock`);
6882
}
6983
}
7084
}

0 commit comments

Comments
 (0)