-
Notifications
You must be signed in to change notification settings - Fork 32
/
singleton.html
58 lines (50 loc) · 2.25 KB
/
singleton.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
<!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 - Singleton Pattern</title>
</head>
<body>
<h1>Interactive HTML Example - Singleton Pattern</h1>
<button id="create-singleton">Create Singleton Instance</button>
<button id="create-bad-singleton">Create Bad Singleton Instance</button>
<button id="check-equality">Check Equality</button>
<div id="output"></div>
<script type="module">
// Usage:
import MySingleton from './modules/MySingleton.mjs';
import MyBadSingleton from './modules/MyBadSingleton.mjs';
const singleA = new MySingleton();
const singleB = new MySingleton();
console.log(singleA.getRandomNumber() === singleB.getRandomNumber());
// true
const badSingleA = new MyBadSingleton();
const badSingleB = new MyBadSingleton();
console.log(badSingleA.getRandomNumber() !== badSingleB.getRandomNumber());
// true
// UI elements
const createSingletonButton = document.getElementById('create-singleton');
const createBadSingletonButton = document.getElementById('create-bad-singleton');
const checkEqualityButton = document.getElementById('check-equality');
const outputElement = document.getElementById('output');
createSingletonButton.addEventListener('click', () => {
const singleton = new MySingleton();
outputElement.textContent = 'Singleton instance created.';
});
createBadSingletonButton.addEventListener('click', () => {
const badSingleton = new MyBadSingleton();
outputElement.textContent = 'Bad Singleton instance created.';
});
checkEqualityButton.addEventListener('click', () => {
const singletonA = new MySingleton();
const singletonB = new MySingleton();
const badSingletonA = new MyBadSingleton();
const badSingletonB = new MyBadSingleton();
const singletonEquality = singletonA.getRandomNumber() === singletonB.getRandomNumber();
const badSingletonEquality = badSingletonA.getRandomNumber() === badSingletonB.getRandomNumber();
outputElement.textContent = `Singleton equality: ${singletonEquality}\nBad Singleton equality: ${badSingletonEquality}`;
});
</script>
</body>
</html>