Abstract factory pattern

概念

抽象工厂模式 提供了一种方式封装一组独立的工厂有一个共同的主题,而无需指定他们的具体类,即提供接口,用于创建相关或依赖对象的类。

举例

之前提到了 工厂方法模式,区分这两种模式的点在于 工厂方法 -> 单一产品,而 抽象工厂 -> 产品族,对于下面的例子,组件工厂类可以抽分为两种工厂,分别为生成高质量组件的工厂(HighQualityFactory) 和 生成廉价组件的工厂(LowQualityFactory),工厂生成的是产品族,一个工厂可以生成多种类别的组件,根据组件的高质量 或 廉价 两种属性确定对应的工厂。

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Created by Heiku on 2018/7/28
*
* model : Abstract factory pattern
*/


// 组件接口
interface PhoneComponents{

// 组件质量
PhoneComponents quailty();
// 组件获取
PhoneComponents get();
}

// 摄像头
abstract class CameraComponent implements PhoneComponents{

@Override
public PhoneComponents get() {
System.out.println("get CameraComponent");
return this;
}
}

// 屏幕
abstract class ScreenComponent implements PhoneComponents{

@Override
public PhoneComponents get() {
System.out.println("get ScreenComponent");
return this;
}
}

// 处理器
abstract class ProcessorComponent implements PhoneComponents{

@Override
public PhoneComponents get() {
System.out.println("get ProcessorComponent");
return this;
}
}

// 高质量 摄像头
class HighQuailtyCamera extends CameraComponent{

@Override
public PhoneComponents quailty() {
System.out.println("this is high quality camera");
return this;
}
}
// 低质量 摄像头
class LowQualityCamera extends CameraComponent{

@Override
public PhoneComponents quailty() {
System.out.println("this is low quality camera");
return this;
}
}


// 高质量 屏幕
class HighQualityScreen extends ScreenComponent{

@Override
public PhoneComponents quailty() {
System.out.println("this is high quality screen");
return this;
}
}
// 低质量 摄像头
class LowQualityScreen extends ScreenComponent{

@Override
public PhoneComponents quailty() {
System.out.println("this is low quality screen");
return this;
}
}


// 高质量 处理器
class HighQualityProcessor extends ProcessorComponent{

@Override
public PhoneComponents quailty() {
System.out.println("this is high quality processor");
return this;
}
}
// 低质量 处理器
class LowQualityProcessor extends ProcessorComponent{

@Override
public PhoneComponents quailty() {
System.out.println("this is low quality processor");
return this;
}
}
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
// 抽象工厂
interface Factory{

PhoneComponents productCamera();
PhoneComponents productScreen();
PhoneComponents productProcessor();
}

// 实体 高级工厂(负责生产高质量产品)
class HighQualityFactory implements Factory{

@Override
public PhoneComponents productCamera() {
return new HighQuailtyCamera();
}

@Override
public PhoneComponents productScreen() {
return new HighQualityScreen();
}

@Override
public PhoneComponents productProcessor() {
return new HighQualityProcessor();
}
}

// 实体 低级工厂(负责生成廉价产品)
class LowQualityFactory implements Factory{

@Override
public PhoneComponents productCamera() {
return new LowQualityCamera();
}

@Override
public PhoneComponents productScreen() {
return new LowQualityScreen();
}

@Override
public PhoneComponents productProcessor() {
return new LowQualityProcessor();
}
}

public class PhoneShop {
public static void main(String[] args) {
Factory highFactory = new HighQualityFactory();
Factory lowFactory = new LowQualityFactory();

// 不同工厂生成
highFactory.productCamera().quailty();
lowFactory.productScreen().quailty();

highFactory.productScreen().quailty();
lowFactory.productScreen().quailty();

highFactory.productProcessor().quailty();
lowFactory.productProcessor().quailty();
}
}