- 重点: 理解什么叫产品簇
- 特点:
- 多个工厂多个产品, 每个工厂都能生产多种产品, 但是每个工厂生产出来的同一个产品特点都不一样
- 优点:
- 新增产品是新增对应的工厂和产品就好了, 不用修改原有的工厂类, 产品之间的创建逻辑解耦
- 缺点:
- 所有的工厂和产品的种类都已经设计好, 难以扩展抽象工厂来生产新种类的产品
该模式重点需要理解产品簇的概念。在增加产品种类时符合开闭原则。 但是在增加产品时不符合开闭原则,反而改起来更加麻烦。
所以在实际项目中使用并不多。
Golang 的实现
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
| package abstract_factory
import "fmt"
// 抽象
type AbstractProductA interface {
Show()
}
type AbstractProductB interface {
Show()
}
type AbstractFactoryA interface {
CreateProductA() AbstractProductA
CreateProductB() AbstractProductB
}
type AbstractFactoryB interface {
CreateProductA() AbstractProductA
CreateProductB() AbstractProductB
}
// 实现
type FactoryAProductA struct {}
func (p FactoryAProductA) Show() {
fmt.Println("FactoryAProductA Show")
}
type FactoryAProductB struct {}
func (p FactoryAProductB) Show() {
fmt.Println("FactoryAProductB Show")
}
type FactoryBProductA struct {}
func (p FactoryBProductA) Show() {
fmt.Println("FactoryBProductA Show")
}
type FactoryBProductB struct {}
func (p FactoryBProductB) Show() {
fmt.Println("FactoryBProductB Show")
}
type FactoryA struct {}
func (f FactoryA) CreateProductA() AbstractProductA {
return &FactoryAProductA{}
}
func (f FactoryA) CreateProductB() AbstractProductB {
return &FactoryAProductB{}
}
type FactoryB struct {}
func (f FactoryB) CreateProductA() AbstractProductA {
return &FactoryBProductA{}
}
func (f FactoryB) CreateProductB() AbstractProductB {
return &FactoryBProductB{}
}
// 业务
/*
factoryAProductA := new(FactoryA).CreateProductA()
factoryAProductB := new(FactoryA).CreateProductB()
factoryBProductA := new(FactoryA).CreateProductA()
factoryBProductB := new(FactoryA).CreateProductB()
*/
|
TypeScript 的实现
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
| interface ProductA {
factory: string
usefulFunctionA(): void
}
interface ProductB {
factory: string
usefulFunctionB(): void
}
class ProductA implements ProductA {
public factory: string
constructor(factory: string){
this.factory = factory
}
usefulFunctionA() {
console.log(`this is ProductA usefulFunctionA, made in ${this.factory}`)
}
}
class ProductB implements ProductB {
public factory: string
constructor(factory: string){
this.factory = factory
}
usefulFunctionB() {
console.log(`this is ProductB usefulFunctionB, made in ${this.factory}`)
}
}
interface Factory{
factoryName: string
createProductA(): ProductA
createProductB(): ProductB
}
class FactoryA implements Factory{
factoryName = 'FactoryA'
createProductA() {
return new ProductA(this.factoryName)
}
createProductB() {
return new ProductB(this.factoryName)
}
}
class FactoryB implements Factory{
factoryName = 'FactoryB'
createProductA() {
return new ProductA(this.factoryName)
}
createProductB() {
return new ProductB(this.factoryName)
}
}
const productA: ProductA = new FactoryA().createProductA()
productA.usefulFunctionA()
const productB: ProductB = new FactoryA().createProductB()
productB.usefulFunctionB()
const productA1: ProductA = new FactoryB().createProductA()
productA1.usefulFunctionA()
const productB1: ProductB = new FactoryB().createProductB()
productB1.usefulFunctionB()
|