-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFactory.js
More file actions
63 lines (57 loc) · 1.43 KB
/
Factory.js
File metadata and controls
63 lines (57 loc) · 1.43 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
53
54
55
56
57
58
59
60
61
62
63
// 简单工厂模式
// Pet 类
class Pet {
// 构造函数
constructor(props) {
this.species = props.species;
this.sound = props.sound;
}
// 静态实例创建方法
static getProps(pet) {
switch (pet) {
case 'dog':
return new Pet({species: 'dog', sound: 'woof'});
case 'cat':
return new Pet({species: 'cat', sound: 'meow'});
case 'bird':
return new Pet({species: 'bird', sound: 'chirping'});
}
}
}
let Adog = Pet.getProps('dog');
console.log(Adog.sound); // woof
let Acat = Pet.getProps('cat');
console.log(Acat.sound); // meow
let Abird = Pet.getProps('bird');
console.log(Abird.sound); // chirping
// 工厂方法模式
class Pet {
constructor(species = '', sound = '') {
this.species = species;
this.sound = sound;
}
}
// 工厂子类
class PetShop extends Pet {
constructor(species, sound) {
super(species, sound);
}
create(pet) {
switch (pet) {
case 'dog':
return new PetShop('dog','woof');
case 'cat':
return new PetShop('cat','meow');
case 'bird':
return new PetShop('bird','chirping');
}
}
}
let thePetShop = new PetShop();
// 通过创建者的方法进行实例创建
let shopDog = thePetShop.create('dog');
console.log(shopDog.sound); // woof
let shopCat = thePetShop.create('cat');
console.log(shopCat.sound); // meow
let shopBird = thePetShop.create('bird');
console.log(shopBird.sound); // chirping