-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract-factory.js
81 lines (66 loc) · 1.81 KB
/
abstract-factory.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
// Poor attempt at abstract factory
var inheritsFrom = function (child, parent) {
child.prototype = Object.create(parent.prototype);
};
/**
@constructor
@abstract
*/
var MobileDeviceAbstract = function() {
if (this.constructor === MobileDeviceAbstract) {
throw new Error("Can't instantiate abstract class!");
}
// MobileDevice initialization...
this.getScreenSize = function(){
throw new Error("Abstract method!");
}
};
MobileDeviceAbstract.prototype.getDeviceName = function(){
throw new Error("Abstract method!");
}
MobileDeviceAbstract.prototype.getScreenSize = function(){
throw new Error("Abstract method!");
}
var PhoneAbstract = function() {
var deviceType = 'phone';
if (this.constructor === PhoneAbstract) {
throw new Error("Can't instantiate abstract class!");
}
MobileDeviceAbstract.apply(this, arguments);
};
PhoneAbstract.prototype = Object.create(MobileDeviceAbstract);
PhoneAbstract.prototype.constructor = PhoneAbstract
var TabletAbstract = function() {
var deviceType = 'tablet';
MobileDeviceAbstract.apply(this, arguments);
};
var ApplePhone = function() {
this.deviceName = 'iPhone';
PhoneAbstract.apply(this, arguments);
}
ApplePhone.prototype = Object.create(PhoneAbstract);
ApplePhone.prototype.constructor = ApplePhone;
ApplePhone.prototype.getDeviceName = function(){
return this.deviceName;
}
ApplePhone.prototype.getScreenSize = function(){
return 'hello';
}
/**
@abstract
*/
//var bad = new PhoneAbstract();
var me = new ApplePhone();
console.log(me);
console.log(me.getDeviceName());
console.log(me.getScreenSize());
/*
var Cat = function() {
Animal.apply(this, arguments);
// Cat initialization...
};
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.constructor = Cat;
Cat.prototype.say = function() {
console.log('meow');
}*/