/
5_20.js
198 lines (171 loc) · 4.83 KB
/
5_20.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class Mechanic {
// prepare_trip(trip) {
// trip.bicycles.forEach((bicycle) => this.prepare_bicycle(bicycle));
// }
// ...
prepare_bicycles(bicycles) {
bicycles.forEach((bicycle) => this.prepare_bicycle(bicycle));
}
prepare_bicycle(bicycle) {
// ...
console.log(`Mechanic prepare_bicycle ${bicycle}`);
}
}
class TripCoordinator {
// prepare_trip(trip) {
// this.buy_food(trip.customers);
// }
// ...
buy_food(customers) {
// ...
console.log('TripCoordinator buy_food');
}
}
class Driver {
// prepare_trip(trip) {
// const vehicle = trip.vehicle;
// this.gas_up(vehicle);
// this.fill_water_tank(vehicle);
// }
// ...
gas_up(vehicle) {
// ...
console.log('Driver gas_up');
}
fill_water_tank(vehicle) {
// ...
console.log('Driver fill_water_tank');
}
}
class Bicycle {}
class Customer {}
class Vehicle {}
/*************************
* Initial example
*************************/
class Trip {
constructor(bicycles, customers, vehicle) {
this._bicycles = bicycles;
this._customers = customers;
this._vehicle = vehicle;
}
get bicycles() { return this._bicycles; }
get customers() { return this._customers; }
get vehicle() { return this._vehicle; }
prepare(preparers) {
preparers.forEach((preparer) => {
switch (preparer.constructor) {
case Mechanic:
preparer.prepare_bicycles(this.bicycles);
break;
case TripCoordinator:
preparer.buy_food(this.customers);
break;
case Driver:
preparer.gas_up(this.vehicle);
preparer.fill_water_tank(this.vehicle);
break;
}
});
}
}
console.log('\nInitial example:');
let trip = new Trip(
[new Bicycle(), new Bicycle(), new Bicycle()],
[new Customer(), new Customer()],
new Vehicle(),
);
let preparers = [
new TripCoordinator(),
new Driver(),
new Mechanic(),
];
trip.prepare(preparers);
/*************************
* instanceof example
* ADAPTATION: instanceof used in place of Ruby's "kind_of?"
*************************/
class TripUsingInstanceOf {
constructor(bicycles, customers, vehicle) {
this._bicycles = bicycles;
this._customers = customers;
this._vehicle = vehicle;
}
get bicycles() { return this._bicycles; }
get customers() { return this._customers; }
get vehicle() { return this._vehicle; }
prepare(preparers) {
preparers.forEach((preparer) => {
if (preparer instanceof Mechanic) {
preparer.prepare_bicycles(this.bicycles);
} else if (preparer instanceof TripCoordinator) {
preparer.buy_food(this.customers);
} else if (preparer instanceof Driver) {
preparer.gas_up(this.vehicle);
preparer.fill_water_tank(this.vehicle);
}
});
}
}
console.log('\ninstanceof example:');
// NOTE: defined above, reassigned here...
trip = new TripUsingInstanceOf(
[new Bicycle(), new Bicycle(), new Bicycle()],
[new Customer(), new Customer()],
new Vehicle(),
);
preparers = [
new TripCoordinator(),
new Driver(),
new Mechanic(),
];
trip.prepare(preparers);
/*************************
* 'in' operator example
*
* ADAPTATION: There are multiple ways to check if an object responds to a
* particular message. Commonly, you'll see code checking to see that a property
* is defined on an object by calling it in an if condition, or using Object's
* 'hasOwnProperty' method.
* This example uses the 'in' operator, because it checks for the existence of
* the property anywhere in the object's prototype chain -- which seems most
* similar to the example using Ruby's 'responds_to?' ... just be aware this
* isn't a great translation.
*
* SEE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
* SEE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty#Description
*************************/
class TripUsingInOperator {
constructor(bicycles, customers, vehicle) {
this._bicycles = bicycles;
this._customers = customers;
this._vehicle = vehicle;
}
get bicycles() { return this._bicycles; }
get customers() { return this._customers; }
get vehicle() { return this._vehicle; }
prepare(preparers) {
preparers.forEach((preparer) => {
if ('prepare_bicycles' in preparer) {
preparer.prepare_bicycles(this.bicycles);
} else if ('buy_food' in preparer) {
preparer.buy_food(this.customers);
} else if ('gas_up' in preparer) {
preparer.gas_up(this.vehicle);
preparer.fill_water_tank(this.vehicle);
}
});
}
}
console.log('\nin operator example:');
trip = new TripUsingInOperator(
[new Bicycle(), new Bicycle(), new Bicycle()],
[new Customer(), new Customer()],
new Vehicle(),
);
preparers = [
new TripCoordinator(),
new Driver(),
new Mechanic(),
];
trip.prepare(preparers);