/
flyweight_2.html
201 lines (184 loc) · 4.67 KB
/
flyweight_2.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
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
199
200
201
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Flyweight Design Pattern Example</title>
</head>
<body>
<h1>Flyweight Design Pattern Example</h1>
<button id="runButton">Run Flyweight</button>
<div id="output"></div>
<script>
// Book class representing individual books
class Book {
constructor(
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN,
checkoutDate,
checkoutMember,
dueReturnDate,
availability
) {
this.id = id;
this.title = title;
this.author = author;
this.genre = genre;
this.pageCount = pageCount;
this.publisherID = publisherID;
this.ISBN = ISBN;
this.checkoutDate = checkoutDate;
this.checkoutMember = checkoutMember;
this.dueReturnDate = dueReturnDate;
this.availability = availability;
}
getTitle() {
return this.title;
}
getAuthor() {
return this.author;
}
getISBN() {
return this.ISBN;
}
updateCheckoutStatus(
bookID,
newStatus,
checkoutDate,
checkoutMember,
newReturnDate
) {
this.id = bookID;
this.availability = newStatus;
this.checkoutDate = checkoutDate;
this.checkoutMember = checkoutMember;
this.dueReturnDate = newReturnDate;
}
extendCheckoutPeriod(bookID, newReturnDate) {
this.id = bookID;
this.dueReturnDate = newReturnDate;
}
isPastDue(bookID) {
const currentDate = new Date();
return currentDate.getTime() > Date.parse(this.dueReturnDate);
}
}
// Flyweight Factory for managing book instances
class BookFactory {
constructor() {
this.books = {};
}
getBook(
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN,
checkoutDate,
checkoutMember,
dueReturnDate,
availability
) {
let book = this.books[id];
if (!book) {
book = new Book(
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN,
checkoutDate,
checkoutMember,
dueReturnDate,
availability
);
this.books[id] = book;
}
return book;
}
getTotalBooks() {
return Object.keys(this.books).length;
}
}
// Button click event
const runButton = document.getElementById('runButton');
runButton.addEventListener('click', function () {
testFlyweight();
});
// Test Flyweight
function testFlyweight() {
const bookFactory = new BookFactory();
const book1 = bookFactory.getBook(
'123',
'The Great Gatsby',
'F. Scott Fitzgerald',
'Classic',
180,
'Penguin',
'978-0141182636',
null,
null,
null,
'Available'
);
const book2 = bookFactory.getBook(
'456',
'To Kill a Mockingbird',
'Harper Lee',
'Classic',
281,
'HarperCollins',
'978-0061120084',
null,
null,
null,
null,
'Available'
);
const book3 = bookFactory.getBook(
'789',
'Harry Potter and the Sorcerer\'s Stone',
'J.K. Rowling',
'Fantasy',
320,
'Scholastic',
'978-0590353427',
null,
null,
null,
'Available'
);
const book4 = bookFactory.getBook(
'123',
'The Great Gatsby',
'F. Scott Fitzgerald',
'Classic',
180,
'Penguin',
'978-0141182636',
null,
null,
null,
'Available'
);
const output = document.getElementById('output');
output.innerHTML = `
<p><strong>Total Books Created:</strong> ${bookFactory.getTotalBooks()}</p>
<h3>Book Details</h3>
<ul>
<li>${book1.getTitle()} by ${book1.getAuthor()} - ISBN: ${book1.getISBN()}</li>
<li>${book2.getTitle()} by ${book2.getAuthor()} - ISBN: ${book2.getISBN()}</li>
<li>${book3.getTitle()} by ${book3.getAuthor()} - ISBN: ${book3.getISBN()}</li>
<li>${book4.getTitle()} by ${book4.getAuthor()} - ISBN: ${book4.getISBN()}</li>
</ul>
`;
}
</script>