-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubject.html
143 lines (120 loc) · 3.18 KB
/
subject.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
<!--
* @Author: Andrew Q
* @Date: 2022-03-13 18:15:34
* @LastEditors: Andrew Q
* @LastEditTime: 2022-03-13 18:22:13
* @Description: 观察者模式
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<header><h3>常规代码</h3></header>
<input type="text" />
<footer>
<div class="box" id="a">A</div>
<div class="box" id="b">B</div>
<!-- 新增需求检查是否包含数字 -->
<div class="box" id="c">包含数字</div>
</footer>
<!-- 观察者模式 -->
<header><h3>观察者模式</h3></header>
<input type="text" id="inputSub" />
<div class="boxs"></div>
</body>
</html>
<script>
// 未使用设计模式 每增加一个需求就要修改 checkActive函数
// 违背面向对象的开闭原则 - 软件中的对象(类,模块,函数等等)应该对于扩展是开放的,但是对于修改是封闭的
document
.querySelector('input')
.addEventListener('input', checkActive)
function checkActive() {
document
.querySelector('#a')
.classList.toggle('active', this.value.includes('a'))
document
.querySelector('#b')
.classList.toggle('active', this.value.includes('b'))
// 新增代码
document
.querySelector('#c')
.classList.toggle('active', /\d/.test(this.value))
}
</script>
<script>
// 使用设计模式- 观察者模式 重构
class Subject {
constructor(el) {
this.observers = []
// 监听input事件
el.addEventListener('input', (e) =>
this.notifyAll(e.target.value)
)
}
// 添加观察者
add(observer) {
this.observers.push(observer)
}
// 通知观察者
notifyAll(value) {
this.observers.forEach((ob) => ob.notify(value))
}
}
// 观察者基类
class OBserver extends DocumentFragment {
constructor(txt) {
super()
this.createDiv(txt)
}
createDiv(txt) {
this.div = document.createElement('div')
this.div.innerText = txt
this.div.className = 'box'
document.querySelector('.boxs').appendChild(this.div)
}
// 执行具体方法
notify(value) {
this.div.classList.toggle('active', this.handel(value))
}
}
// 检查是否包含字母子类
class DefaultObserver extends OBserver {
constructor(txt) {
super(txt)
this.txt = txt
}
handel(value) {
return value.includes(this.txt)
}
}
const sub = new Subject(document.querySelector('#inputSub'))
sub.add(new DefaultObserver('A'))
sub.add(new DefaultObserver('B'))
// 新增需求检查是否包含数字
// 1 新增包含数字子类不需要修改原代码 符合扩展开放原则 完全解耦
// 2 调用
class NumberObserver extends OBserver {
constructor(txt) {
super(txt)
this.txt = txt
}
handel(value) {
return /\d/.test(value)
}
}
sub.add(new NumberObserver('包含数字'))
</script>