-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstract-Factory-Pattern.js
More file actions
46 lines (40 loc) · 1012 Bytes
/
Abstract-Factory-Pattern.js
File metadata and controls
46 lines (40 loc) · 1012 Bytes
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
// 抽象工厂模式(Abstract Factory Pattern)
class User {
constructor (type) {
if (new.target === User) {
throw new Error('抽象类不能实例化!')
}
this.type = type
}
}
class UserOfWechat extends User {
constructor (name) {
super('wechat')
this.name = name
this.viewPage = ['首页', '通讯录', '发现页']
}
}
class UserOfQQ extends User {
constructor (name) {
super('qq')
this.name = name
this.viewPage = ['首页', '通讯录', '发现页']
}
}
function getUser (type) {
switch (type) {
case 'wechat':
return UserOfWechat
break
case 'qq':
return UserOfQQ
break
default:
throw new Error('参数错误')
break
}
}
let WechatUserClass = getUser('wechat')
let QQUserClass = getUser('qq')
let wechatUser = new WechatUserClass('卡卡罗特')
let qqUser = new QQUserClass('贝吉塔')