-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_es5.js
50 lines (41 loc) · 998 Bytes
/
factory_es5.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
// Transport class
function Transport(how) {
this.logger = console
this._how = how;
}
Transport.prototype.move = function () {
this.logger.log('Moving in the {0}!!'.replace('{0}', this._how));
};
Object.defineProperty(Transport.prototype, "how", {
get: function () {
return this._how;
},
set: function (value) {
this._how = value;
}
});
// Drone class
function Drone(how) {
how = how || 'air';
Transport.call(this, how);
}
Drone.prototype = Object.create(Transport.prototype);
// Car class
function Car(how) {
how = how || 'ground';
Transport.call(this, how);
}
Car.prototype = Object.create(Transport.prototype);
function TransportFactory() {}
TransportFactory.getTransport = function (transportType) {
if (transportType == null) {
return null;
} else if (transportType === 'car') {
return new Car();
} else if (transportType === 'drone') {
return new Drone();
} else {
return null;
}
}
TransportFactory.getTransport('car').move();