-
Notifications
You must be signed in to change notification settings - Fork 0
/
method.ts
65 lines (57 loc) · 1.72 KB
/
method.ts
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
/**
* 工厂方法
* 就是把创建产品的工作交由具体工厂类来实现
*/
abstract class Coffee {
constructor(public name: string) {}
}
class AmericanCoffee extends Coffee {}
class LatteCoffee extends Coffee {}
class CappuccinoCoffee extends Coffee {}
abstract class CateFactory {
abstract createCoffee(): Coffee;
}
class AmericanCoffeeFactory extends CateFactory {
createCoffee(): Coffee {
return new AmericanCoffee('美式咖啡');
}
}
class LatteCoffeeFactory extends CateFactory {
createCoffee(): Coffee {
return new LatteCoffee('拿铁咖啡');
}
}
class CappuccinoCoffeeFactory extends CateFactory {
createCoffee(): Coffee {
return new CappuccinoCoffee('卡布其诺咖啡');
}
}
// 原来在简单工厂里,是由Factory来创建产品的
// 在工厂方法里,不再由Factory 来创建产品,而是先创建具体的工厂,然后具体的工厂来创建产品
class Factory {
static order(name: string) {
switch (name) {
case 'AmericanCoffee':
return new AmericanCoffeeFactory().createCoffee();
case 'LatteCoffee':
return new LatteCoffeeFactory().createCoffee();
case 'CappuccinoCoffee':
return new CappuccinoCoffeeFactory().createCoffee();
default:
throw new Error('你点的咖啡品种我们不支持');
}
}
}
console.log(Factory.order('AmericanCoffee'));
function createElement(type: any, config: any) {
// this 绑定为null 第一个参数绑定type
return { type, props: config };
}
function createFactory(type: string) {
const factory = createElement.bind(null, type);
return factory;
}
let factory = createFactory('h1');
let element = factory({ id: 'h1', className: 'title' });
console.log(element);
export {};