设计模式之抽象工厂模式20170803

创建型设计模式之抽象工厂模式:

一、含义

为创建一组相关或相互依赖的对象提供一个接口,而且无需指定它们的具体类。

具体来说,

为一组具有相同约束(属性)的对象,提供一个接口,这个接口下有不同的实现,每个实现类对应一种类型的约束(一种具体的属性),同时提供该类型的约束(属性)下所有对象的创建方法

二、代码说明

1.主要有两个角色

1)一组互相影响的产品线(对象),也叫做产品族

2)抽象工厂类及其实现类

抽象工厂类:在N个产品族中,在抽象工厂类中就应该有N个创建方法

实现类:具体实现类是产品族的具体属性(约束条件)的体现,每个实现类都能创建所有的产品族。

例如,有M个产品等级就应该有M个实现工厂类(约束条件是产品等级),在每个实现工厂中,实现相应等级的所有产品族的生产任务。

2.在用C实现过程中也是参考这种思想,以创造男人、女人举例,具体实现如下:

1)抽象工厂模式使用场景:

 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     AbstractFactoryPatternUsage.c
 5 * Description        :     抽象工厂模式的使用
 6 
 7 book@book-desktop:/work/projects/test/DesignPatterns/AbstractFactoryPattern$ gcc -o AbstractFactoryPatternUsage MaleYellowHuman.c MaleBlackHuman.c  MaleWhiteHuman.c BlackHuman.c WhiteHuman.c YellowHuman.c FemaleBlackHuman.c FemaleYellowHuman.c FemaleWhiteHuman.c FemaleHumanFactory.c MaleHumanFactory.c AbstractFactoryPattern.c AbstractFactoryPatternUsage.c 
 8 book@book-desktop:/work/projects/test/DesignPatterns/AbstractFactoryPattern$ ./AbstractFactoryPatternUsage 
 9 ------------创造一个黄色女性:------------
10 黄色人种的皮肤是黄色的
11 黄色人种会说话,一般说的是双字节
12 黄人女性
13 ------------创造一个黄色男性:------------
14 黄色人种的皮肤是黄色的
15 黄色人种会说话,一般说的是双字节
16 黄人男性
17 
18 * Created            :     2017.08.02.
19 * Author            :     Yu Weifeng
20 * Function List         :     
21 * Last Modified     :     
22 * History            :     
23 ******************************************************************************/
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "AbstractFactoryPattern.h"
28 
29 
30 
31 
32 /*****************************************************************************
33 -Fuction        : main
34 -Description    : 
35 -Input            : 
36 -Output         : 
37 -Return         : 
38 * Modify Date      Version         Author           Modification
39 * -----------------------------------------------
40 * 2017/08/02    V1.0.0         Yu Weifeng       Created
41 ******************************************************************************/
42 int main(int argc,char **argv)
43 {
44     T_HumanFactory tFemaleHumanFactory=newFemaleHumanFactory;
45     T_HumanFactory tMaleHumanFactory=newMaleHumanFactory;
46 
47     T_Human tMaleYellowHuman =tMaleHumanFactory.CreateYellowHuman();
48     T_Human tFemaleYellowHuman =tFemaleHumanFactory.CreateYellowHuman();
49     printf("------------创造一个黄色女性:------------
");
50     tFemaleYellowHuman.GetColor();
51     tFemaleYellowHuman.Talk();
52     tFemaleYellowHuman.GetSex();
53     printf("------------创造一个黄色男性:------------
");
54     tMaleYellowHuman.GetColor();
55     tMaleYellowHuman.Talk();
56     tMaleYellowHuman.GetSex();
57     
58     return 0;
59 }
AbstractFactoryPatternUsage.c

2)被调用者:

 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     AbstractFactoryPattern.c
 5 * Description        :     抽象工厂模式
 6 
 7                         以创造男人、女人举例    
 8                         
 9 * Created            :     2017.08.02.
10 * Author            :     Yu Weifeng
11 * Function List         :     
12 * Last Modified     :     
13 * History            :     
14 ******************************************************************************/
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "AbstractFactoryPattern.h"
AbstractFactoryPattern.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     AbstractFactoryPattern.h
 5 * Description        :     抽象工厂模式
 6                                     
 7 * Created            :     2017.08.02.
 8 * Author            :     Yu Weifeng
 9 * Function List         :     
10 * Last Modified     :     
11 * History            :     
12 ******************************************************************************/
13 #ifndef ABSTRACT_FACTORY_PATTERN_H
14 #define ABSTRACT_FACTORY_PATTERN_H
15 
16 
17 typedef struct Human
18 {
19     void (*GetColor)();
20     void (*Talk)();
21     void (*GetSex)();
22 }T_Human;
23 
24 typedef struct HumanFactory
25 {
26     T_Human (*CreateYellowHuman)();
27     T_Human (*CreateWhiteHuman)();
28     T_Human (*CreateBlackHuman)();
29 }T_HumanFactory;
30 
31 T_Human MaleHumanFactoryCreateYellowHuman();
32 T_Human MaleHumanFactoryCreateWhiteHuman();
33 T_Human MaleHumanFactoryCreateBlackHuman();
34 #define newMaleHumanFactory {MaleHumanFactoryCreateYellowHuman,MaleHumanFactoryCreateWhiteHuman,MaleHumanFactoryCreateBlackHuman}
35 
36 T_Human FemaleHumanFactoryCreateYellowHuman();
37 T_Human FemaleHumanFactoryCreateWhiteHuman();
38 T_Human FemaleHumanFactoryCreateBlackHuman();
39 #define newFemaleHumanFactory {FemaleHumanFactoryCreateYellowHuman,FemaleHumanFactoryCreateWhiteHuman,FemaleHumanFactoryCreateBlackHuman}
40 
41 
42 
43 #endif
AbstractFactoryPattern.h
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     BlackHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是黑人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : YellowHumanGetColor
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void BlackHumanGetColor()
32 {
33     printf("黑色人种的皮肤是黑色的
");
34 }
35 
36 /*****************************************************************************
37 -Fuction        : YellowHumanTalk
38 -Description    : 公有函数
39 -Input            : 
40 -Output         : 
41 -Return         : 
42 * Modify Date      Version         Author           Modification
43 * -----------------------------------------------
44 * 2017/08/02    V1.0.0         Yu Weifeng       Created
45 ******************************************************************************/
46 void BlackHumanTalk()
47 {
48     printf("黑色人种会说话,一般人听不懂
");
49 }
BlackHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     WhiteHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是白人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : WhiteHumanGetColor
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void WhiteHumanGetColor()
32 {
33     printf("白色人种的皮肤是白色的
");
34 }
35 
36 /*****************************************************************************
37 -Fuction        : WhiteHumanGetColor
38 -Description    : 公有函数
39 -Input            : 
40 -Output         : 
41 -Return         : 
42 * Modify Date      Version         Author           Modification
43 * -----------------------------------------------
44 * 2017/08/02    V1.0.0         Yu Weifeng       Created
45 ******************************************************************************/
46 void WhiteHumanTalk()
47 {
48     printf("白色人种会说话,一般说的是单字节
");
49 }
WhiteHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     YellowHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是黄人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : YellowHumanGetColor
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void YellowHumanGetColor()
32 {
33     printf("黄色人种的皮肤是黄色的
");
34 }
35 
36 /*****************************************************************************
37 -Fuction        : YellowHumanTalk
38 -Description    : 公有函数
39 -Input            : 
40 -Output         : 
41 -Return         : 
42 * Modify Date      Version         Author           Modification
43 * -----------------------------------------------
44 * 2017/08/02    V1.0.0         Yu Weifeng       Created
45 ******************************************************************************/
46 void YellowHumanTalk()
47 {
48     printf("黄色人种会说话,一般说的是双字节
");
49 }
YellowHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     FemaleBlackHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是女性黑人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : FemaleBlackHumanGetSex
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void FemaleBlackHumanGetSex()
32 {
33     printf("黑人女性
");
34 }
FemaleBlackHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     FemaleWhiteHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是女性白人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : FemaleWhiteHumanGetSex
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void FemaleWhiteHumanGetSex()
32 {
33     printf("白人女性
");
34 }
FemaleWhiteHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     FemaleYellowHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是女性黄人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : FemaleYellowHumanGetSex
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void FemaleYellowHumanGetSex()
32 {
33     printf("黄人女性
");
34 }
FemaleYellowHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     MaleBlackHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是男性黑人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : MaleBlackHumanGetSex
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void MaleBlackHumanGetSex()
32 {
33     printf("黑人男性
");
34 }
MaleBlackHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     MaleWhiteHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是男性白人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : MaleWhiteHumanGetSex
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void MaleWhiteHumanGetSex()
32 {
33     printf("白人男性
");
34 }
MaleWhiteHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     MaleYellowHuman.c
 5 * Description        :     抽象工厂模式
 6                         本文件是男性黄人的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 
20 
21 /*****************************************************************************
22 -Fuction        : MaleYellowHumanGetSex
23 -Description    : 公有函数
24 -Input            : 
25 -Output         : 
26 -Return         : 
27 * Modify Date      Version         Author           Modification
28 * -----------------------------------------------
29 * 2017/08/02    V1.0.0         Yu Weifeng       Created
30 ******************************************************************************/
31 void MaleYellowHumanGetSex()
32 {
33     printf("黄人男性
");
34 }
MaleYellowHuman.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     FemaleHumanFactory.c
 5 * Description        :     抽象工厂模式
 6                         本文件是女性人类工厂(创造女性)的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 void BlackHumanGetColor();
20 void BlackHumanTalk();
21 
22 void WhiteHumanGetColor();
23 void WhiteHumanTalk();
24 
25 void YellowHumanGetColor();
26 void YellowHumanTalk();
27 
28 void FemaleBlackHumanGetSex();
29 #define newFemaleBlackHuman {BlackHumanGetColor,BlackHumanTalk,FemaleBlackHumanGetSex}
30 
31 void FemaleWhiteHumanGetSex();
32 #define newFemaleWhiteHuman {WhiteHumanGetColor,WhiteHumanTalk,FemaleWhiteHumanGetSex}
33 
34 void FemaleYellowHumanGetSex();
35 #define newFemaleYellowHuman {YellowHumanGetColor,YellowHumanTalk,FemaleYellowHumanGetSex}
36 
37 /*****************************************************************************
38 -Fuction        : FemaleHumanFactoryCreateYellowHuman
39 -Description    : 公有函数
40 -Input            : 
41 -Output         : 
42 -Return         : 
43 * Modify Date      Version         Author           Modification
44 * -----------------------------------------------
45 * 2017/08/02    V1.0.0         Yu Weifeng       Created
46 ******************************************************************************/
47 T_Human FemaleHumanFactoryCreateYellowHuman()
48 {
49     T_Human tHuman=newFemaleYellowHuman;
50     return tHuman;
51 }
52 
53 /*****************************************************************************
54 -Fuction        : FemaleHumanFactoryCreateWhiteHuman
55 -Description    : 公有函数
56 -Input            : 
57 -Output         : 
58 -Return         : 
59 * Modify Date      Version         Author           Modification
60 * -----------------------------------------------
61 * 2017/08/02    V1.0.0         Yu Weifeng       Created
62 ******************************************************************************/
63 T_Human FemaleHumanFactoryCreateWhiteHuman()
64 {
65     T_Human tHuman=newFemaleWhiteHuman;
66     return tHuman;
67 }
68 
69 /*****************************************************************************
70 -Fuction        : FemaleHumanFactoryCreateBlackHuman
71 -Description    : 公有函数
72 -Input            : 
73 -Output         : 
74 -Return         : 
75 * Modify Date      Version         Author           Modification
76 * -----------------------------------------------
77 * 2017/08/02    V1.0.0         Yu Weifeng       Created
78 ******************************************************************************/
79 T_Human FemaleHumanFactoryCreateBlackHuman()
80 {
81     T_Human tHuman=newFemaleBlackHuman;
82     return tHuman;
83 }
FemaleHumanFactory.c
 1 /*****************************************************************************
 2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
 3 ------------------------------------------------------------------------------
 4 * File Module        :     MaleHumanFactory.c
 5 * Description        :     抽象工厂模式
 6                         本文件是男性人类工厂(创造男性)的具体实现
 7                         
 8 * Created            :     2017.08.02.
 9 * Author            :     Yu Weifeng
10 * Function List         :     
11 * Last Modified     :     
12 * History            :     
13 ******************************************************************************/
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "AbstractFactoryPattern.h"
18 
19 void BlackHumanGetColor();
20 void BlackHumanTalk();
21 
22 void WhiteHumanGetColor();
23 void WhiteHumanTalk();
24 
25 void YellowHumanGetColor();
26 void YellowHumanTalk();
27 
28 void MaleBlackHumanGetSex();
29 #define newMaleBlackHuman {BlackHumanGetColor,BlackHumanTalk,MaleBlackHumanGetSex}
30 
31 void MaleWhiteHumanGetSex();
32 #define newMaleWhiteHuman {WhiteHumanGetColor,WhiteHumanTalk,MaleWhiteHumanGetSex}
33 
34 void MaleYellowHumanGetSex();
35 #define newMaleYellowHuman {YellowHumanGetColor,YellowHumanTalk,MaleYellowHumanGetSex}
36 
37 /*****************************************************************************
38 -Fuction        : MaleHumanFactoryCreateYellowHuman
39 -Description    : 公有函数
40 -Input            : 
41 -Output         : 
42 -Return         : 
43 * Modify Date      Version         Author           Modification
44 * -----------------------------------------------
45 * 2017/08/02    V1.0.0         Yu Weifeng       Created
46 ******************************************************************************/
47 T_Human MaleHumanFactoryCreateYellowHuman()
48 {
49     T_Human tHuman=newMaleYellowHuman;
50     return tHuman;
51 }
52 
53 /*****************************************************************************
54 -Fuction        : MaleHumanFactoryCreateWhiteHuman
55 -Description    : 公有函数
56 -Input            : 
57 -Output         : 
58 -Return         : 
59 * Modify Date      Version         Author           Modification
60 * -----------------------------------------------
61 * 2017/08/02    V1.0.0         Yu Weifeng       Created
62 ******************************************************************************/
63 T_Human MaleHumanFactoryCreateWhiteHuman()
64 {
65     T_Human tHuman=newMaleWhiteHuman;
66     return tHuman;
67 }
68 
69 /*****************************************************************************
70 -Fuction        : MaleHumanFactoryCreateBlackHuman
71 -Description    : 公有函数
72 -Input            : 
73 -Output         : 
74 -Return         : 
75 * Modify Date      Version         Author           Modification
76 * -----------------------------------------------
77 * 2017/08/02    V1.0.0         Yu Weifeng       Created
78 ******************************************************************************/
79 T_Human MaleHumanFactoryCreateBlackHuman()
80 {
81     T_Human tHuman=newMaleBlackHuman;
82     return tHuman;
83 }
MaleHumanFactory.c

3)执行结果:

book@book-desktop:/work/projects/test/DesignPatterns/AbstractFactoryPattern$ gcc -o AbstractFactoryPatternUsage MaleYellowHuman.c MaleBlackHuman.c  MaleWhiteHuman.c BlackHuman.c WhiteHuman.c YellowHuman.c FemaleBlackHuman.c FemaleYellowHuman.c FemaleWhiteHuman.c FemaleHumanFactory.c MaleHumanFactory.c AbstractFactoryPattern.c AbstractFactoryPatternUsage.c

book@book-desktop:/work/projects/test/DesignPatterns/AbstractFactoryPattern$ ./AbstractFactoryPatternUsage

------------创造一个黄色女性:------------

黄色人种的皮肤是黄色的

黄色人种会说话,一般说的是双字节

黄人女性

------------创造一个黄色男性:------------

黄色人种的皮肤是黄色的

黄色人种会说话,一般说的是双字节

黄人男性

4)详细代码:

https://github.com/fengweiyu/DesignThinking/tree/master/DesignPatterns/CreationalDesignPatterns/AbstractFactoryPattern

三、使用场景

1.一个对象族(或是一组没有任何关系的对象)都有相同的约束(属性),则可以使用抽象工厂模式。

可以使用抽象工厂模式产生不同类型的约束条件的对象。

例如上述例子中,人种都约束于性别这个条件,所以使用抽象工厂模式,有两种性别男和女,所以有两个具体的工厂类,男工厂产生约束条件是男性的人种,女工厂产生约束条件是女性的人种。

四、优点

1.封装性

每个产品的实现类不是高层模块要关心的,只要知道工厂类是谁,就能创建出一个需要的对象。

2.产品族内的约束为非公开状态

生产过程对调用工厂类的高层模块来说是透明的,它不需要知道这个约束(就是要一个黄色女性产品就可以了),具体的产品族内的约束是在工厂内实现的。

五、缺点

1.最大缺点就是产品族扩展非常困难

如果要增加产品家族,那么抽象类要增加一个方法,然后两个实现类也要修改,严重违反了开闭原则,而且修改抽象类是不允许的,抽象类和接口是一个契约,改变契约,所有与契约有关的代码都要修改,这显然是不行的。

注意:

是产品族扩展困难,而不是产品等级。产品等级在该模式下是非常容易扩展的,只要增加一个工厂类负责新增加出来的产品生产任务即可。也就是说横向扩展容易,纵向扩展困难。

六、与其他模式的区别

1、抽象工厂模式与工厂方法模式

1)抽象工厂模式是工厂方法模式的升级版本,抽象工厂模式对工厂进行抽象,具体工厂类之间的属性(约束条件)不同(创造出来的产品(对象)的具体属性不同),每个具体工厂类都有各自的属性。

2)抽象工厂模式产品族扩展非常困难,也就是说横向扩展容易,纵向扩展困难。而工厂方法模式只有一个扩展方向,所以不存在这个问题

2、抽象工厂模式与建造者模式

抽象工厂模式实现对产品家族的创建,一个产品家族是这样的一系列产品:具有不同分类维度的产品组合,采用抽象工厂模式则是不需要关系构建过程,只关心什么产品由什么工厂生成即可。

而建造者模式则是要求按照指定的蓝图建造产品,它的主要目的是通过组装零配件而产生一个新产品,两者的区别还是比较明显的。

具体来说,

1)抽象工厂模式就好比是一个一个的工厂,对外界来说,只要关心一个工厂到底是生产什么产品的,不用关心具体怎么生产。

而建造者模式就不同了,它是由车间组成,不同的车间完成不同的创建和装配任务,车间主任(导演类)给建造车间什么蓝图就能生产什么产品,建造者模式更关心建造过程。

2)相对来说,抽象工厂模式比建造者模式的尺度要大,它关注产品整体,而建造者模式关注构建过程,因此建造者模式可以很容易地构建出一个崭新的产品,只要导演类能够提供具体的工艺流程。

3)两者的应用场景截然不同,如果希望屏蔽对象的创建过程,只提供一个封装良好的对象,则可以选择抽象工厂方法模式。

而建造者模式可以用在构件的装配方面,如通过装配不同的组件或相同组件的不同顺序,可以产生出一个新的对象,它可以产生一个非常灵活的架构,方便地扩展和维护系统。

原文地址:https://www.cnblogs.com/yuweifeng/p/7281656.html