-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdecorators_interface_2.html
More file actions
180 lines (153 loc) · 5.14 KB
/
Copy pathdecorators_interface_2.html
File metadata and controls
180 lines (153 loc) · 5.14 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>MacBook Decorators Example</title>
</head>
<body>
<h1>MacBook Decorators Example</h1>
<form id="decoratorForm">
<label for="engravingCheckbox">Engraving</label>
<input type="checkbox" id="engravingCheckbox" />
<br />
<label for="parallelsCheckbox">Parallels</label>
<input type="checkbox" id="parallelsCheckbox" />
<br />
<label for="ramSelect">RAM:</label>
<select id="ramSelect">
<option value="4">4GB</option>
<option value="8">8GB</option>
</select>
<br />
<label for="caseCheckbox">Case</label>
<input type="checkbox" id="caseCheckbox" />
<br />
<button type="submit">Create MacBook</button>
</form>
<div id="macBookOutput"></div>
<script>
// Interface constructor
function Interface(name, methods) {
if (arguments.length !== 2) {
throw new Error('Interface constructor called with ' + arguments.length + ' arguments, but expected exactly 2.');
}
this.name = name;
this.methods = [];
for (let i = 0; i < methods.length; i++) {
if (typeof methods[i] !== 'string') {
throw new Error('Interface constructor expects method names to be passed in as strings.');
}
this.methods.push(methods[i]);
}
}
// Ensure implementation of an interface
Interface.ensureImplements = function (object, ...interfaces) {
if (arguments.length < 2) {
throw new Error('Function Interface.ensureImplements called with ' + arguments.length + ' arguments, but expected at least 2.');
}
for (let i = 0; i < interfaces.length; i++) {
const interfaceInstance = interfaces[i];
if (interfaceInstance.constructor !== Interface) {
throw new Error('Function Interface.ensureImplements expects arguments two and above to be instances of Interface.');
}
for (let j = 0; j < interfaceInstance.methods.length; j++) {
const method = interfaceInstance.methods[j];
if (!object[method] || typeof object[method] !== 'function') {
throw new Error('Function Interface.ensureImplements: object does not implement the ' + interfaceInstance.name + ' interface. Method ' + method + ' was not found.');
}
}
}
};
// Define MacBook interface
const MacBook = new Interface('MacBook', [
'addEngraving',
'addParallels',
'add4GBRam',
'add8GBRam',
'addCase',
'getPrice',
]);
// MacBook Pro class
class MacBookPro {
addEngraving() {
return 'Added Engraving';
}
addParallels() {
return 'Added Parallels';
}
add4GBRam() {
return 'Added 4GB RAM';
}
add8GBRam() {
return 'Added 8GB RAM';
}
addCase() {
return 'Added Case';
}
getPrice() {
return 900.0;
}
}
// Abstract Decorator class
class MacBookDecorator {
constructor(macbook) {
Interface.ensureImplements(macbook, MacBook);
this.macbook = macbook;
}
addEngraving() {
return this.macbook.addEngraving();
}
addParallels() {
return this.macbook.addParallels();
}
add4GBRam() {
return this.macbook.add4GBRam();
}
add8GBRam() {
return this.macbook.add8GBRam();
}
addCase() {
return this.macbook.addCase();
}
getPrice() {
return this.macbook.getPrice();
}
}
// Decorator form
const decoratorForm = document.getElementById('decoratorForm');
decoratorForm.addEventListener('submit', function (event) {
event.preventDefault();
const engravingCheckbox = document.getElementById('engravingCheckbox');
const parallelsCheckbox = document.getElementById('parallelsCheckbox');
const ramSelect = document.getElementById('ramSelect');
const caseCheckbox = document.getElementById('caseCheckbox');
let macBook = new MacBookPro();
if (engravingCheckbox.checked) {
macBook = new MacBookDecorator(macBook);
}
if (parallelsCheckbox.checked) {
macBook = new MacBookDecorator(macBook);
}
if (ramSelect.value === '4') {
macBook = new MacBookDecorator(macBook);
}
if (caseCheckbox.checked) {
macBook = new MacBookDecorator(macBook);
}
displayMacBook(macBook);
});
// Display MacBook details
function displayMacBook(macBook) {
const output = document.getElementById('macBookOutput');
output.innerHTML = `
<p>${macBook.addEngraving()}</p>
<p>${macBook.addParallels()}</p>
<p>${macBook.add4GBRam()}</p>
<p>${macBook.add8GBRam()}</p>
<p>${macBook.addCase()}</p>
<p><strong>Price:</strong> $${macBook.getPrice()}</p>
`;
}
</script>
</body>
</html>