/
factory_trucks1.html
109 lines (96 loc) · 2.94 KB
/
factory_trucks1.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive HTML Example - Factory Design Pattern</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
margin-bottom: 20px;
}
.output-container {
padding: 10px;
background-color: #f2f2f2;
border-radius: 5px;
font-weight: bold;
color: #333;
}
</style>
</head>
<body>
<h1>Interactive HTML Example - Factory Design Pattern</h1>
<div class="output-container" id="output">
Click the buttons to create different vehicles using the factory.
</div>
<button id="create-car-btn">Create Car</button>
<button id="create-truck-btn">Create Truck</button>
<button id="create-moving-truck-btn">Create Moving Truck</button>
<script>
class Car {
constructor({ doors = 4, state = 'brand new', color = 'silver' } = {}) {
this.doors = doors;
this.state = state;
this.color = color;
}
}
class Truck {
constructor({ state = 'used', wheelSize = 'large', color = 'blue' } = {}) {
this.state = state;
this.wheelSize = wheelSize;
this.color = color;
}
}
class VehicleFactory {
constructor() {
this.vehicleClass = Car;
}
createVehicle(options) {
const { vehicleType, ...rest } = options;
switch (vehicleType) {
case 'car':
this.vehicleClass = Car;
break;
case 'truck':
this.vehicleClass = Truck;
break;
// defaults to VehicleFactory.prototype.vehicleClass (Car)
}
return new this.vehicleClass(rest);
}
}
const vehicleOutputElement = document.getElementById('output');
const createCarButton = document.getElementById('create-car-btn');
const createTruckButton = document.getElementById('create-truck-btn');
const createMovingTruckButton = document.getElementById('create-moving-truck-btn');
const vehicleFactory = new VehicleFactory();
createCarButton.addEventListener('click', () => {
const car = vehicleFactory.createVehicle({
vehicleType: 'car',
color: 'yellow',
doors: 6,
});
vehicleOutputElement.textContent = JSON.stringify(car, null, 2);
});
createTruckButton.addEventListener('click', () => {
const truck = vehicleFactory.createVehicle({
vehicleType: 'truck',
color: 'red',
wheelSize: 'medium',
});
vehicleOutputElement.textContent = JSON.stringify(truck, null, 2);
});
createMovingTruckButton.addEventListener('click', () => {
const movingTruck = vehicleFactory.createVehicle({
vehicleType: 'truck',
state: 'like new',
color: 'red',
wheelSize: 'small',
});
vehicleOutputElement.textContent = JSON.stringify(movingTruck, null, 2);
});
</script>
</body>
</html>