-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.js
More file actions
52 lines (43 loc) · 1.06 KB
/
factory.js
File metadata and controls
52 lines (43 loc) · 1.06 KB
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
class SimpleMembership{
constructor(name){
this.name = name
this.cost = 50
}
}
class StandardMembership{
constructor(name){
this.name = name
this.cost = 150
}
}
class PremiumMembership{
constructor(name){
this.name = name
this.cost = 500
}
}
class MemberFactory {
static list = {
simple: SimpleMembership,
standard: StandardMembership,
premium: PremiumMembership
}
create(name, type='simple'){
const Membership = MemberFactory.list[type] || MemberFactory.list.simple
const member = new Membership(name)
member.type = type
member.define = function() {
console.log(`${this.name} (${this.type}): ${this.cost}`)
}
return member
}
}
const factory = new MemberFactory()
const members = [
factory.create('Ivan', 'simple'),
factory.create('Masha', 'premium'),
factory.create('Max', 'standard'),
factory.create('Dasha', 'premium'),
factory.create('Vika')
]
members.forEach( m => { m.define()})