设计模式学习(一):抽象工厂模式以及C语言示例

一、前言

近期有幸参加了李先静李老师组织的设计模式研讨会,与各位同事共同学习探讨设计模式的相关知识,往后的大多数的博客内容将会围绕设计模式的学习展开。

研讨会内容参考书籍《设计模式——可复用面向对象软件的基础 》,博客中也会大量引用这本书里的定义,但这本书的翻译非常晦涩难读,在博客中我尽量用一些通俗的描述代替,也方便以后自己查阅。

李老师博客地址:http://blog.csdn.net/absurd

二、抽象工厂模式

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。它属于创建型模式,提供了一种比较好的创建对象的方式。

简单理解:工厂是对具体产品(对象)的抽象,而抽象工厂则是对这些具体工厂的抽象。工厂提供统一接口创建不同的产品,而抽象工厂提供统一接口创建不同的工厂。

目的:抽象工厂模式其实想干的事情就是给用户提供一个创建一系列相关或相互依赖对象的接口,但用户是不需要指定它们的具体类。

通过以上简单的介绍,可以知道抽象工厂模式主要解决的是接口选择的问题,接下来我们来看一个示例。

三、示例类图及C语言代码

3.1 示例介绍

假如要实现一个画笔功能,可以画不同的形状(shape)、填充不同的颜色(color)。

形状:方形(square)、圆形(circle);
功能:绘制(draw)。

颜色:红色(red)、蓝色(blue);
功能:填充(fill)。

3.2 示例类图

根据以上描述,使用抽象工厂模式的类图如下:

在这里插入图片描述

首先需要创建 shape_t 和 color_t 接口和实现这些接口的实体类。下一步是创建抽象工厂类 brush_factory_t。接着定义工厂类 shape_factory_t 和 color_factory_t,这两个工厂类都继承了抽象工厂类 brush_factory_t。然后创建一个工厂生成器 factory_froducer_t ,该接口提供给用户使用。

3.3 C语言实现

1、创建 shape_t 接口并完成具体实现(square_t、circle_t):

/************************ shape_t *************************/
typedef void(*draw_t)();
typedef struct _shape_t {
    draw_t draw;
}shape_t;

/* square_t implement shape_t */
typedef struct _square_t {
    shape_t shape;
}square_t;

static void square_draw() {
    printf("square_draw!\n");
}

square_t* square_create() {
    square_t* square = malloc(sizeof(square_t));
    square->shape.draw = square_draw;
    return square;
}

/* circle_t implement shape_t */
typedef struct _circle_t {
    shape_t shape;
}circle_t;

static void circle_draw() {
    printf("circle_draw!\n");
}

circle_t* circle_create() {
    circle_t* circle = malloc(sizeof(circle_t));
    circle->shape.draw = circle_draw;
    return circle;
}

2、创建 color_t 接口并完成具体实现(red_t、blue_t):

/************************* color_t *************************/
typedef void(*fill_t)();
typedef struct _color_t {
    fill_t fill;
}color_t;

/* red_t implement color_t */
typedef struct _red_t {
    color_t color;
}red_t;

static void red_fill() {
    printf("red_fill!\n");
}

red_t* red_create() {
    red_t* red = malloc(sizeof(red_t));
    red->color.fill = red_fill;
    return red;
}

/* blue_t implement color_t */
typedef struct _blue_t {
    color_t color;
}blue_t;

static void blue_fill() {
    printf("blue_fill!\n");
}

red_t* blue_create() {
    blue_t* blue = malloc(sizeof(blue_t));
    blue->color.fill = blue_fill;
    return blue;
}

3、为 shape_t 和 color_t 创建抽象工厂( brush_factory_t )来获取具体的工厂:

/************************* brush_factory_t (Abstract Factory) *************************/
typedef shape_t* (*brush_shape_create_t)(char* shape);
typedef void(*brush_shape_destory_t)(shape_t* shape);
typedef color_t* (*brush_color_create_t)(char* color);
typedef void(*brush_color_destroy_t)(color_t* color);

typedef struct _brush_factory_t {
    brush_shape_create_t brush_shape_create;
    brush_shape_destory_t brush_shape_destory;
    brush_color_create_t brush_color_create;
    brush_color_destroy_t brush_color_destroy;
}brush_factory_t;

4、实现具体工厂 shape_factory_t 和 color_factory_t :

/* shape_factory_t implement brush_factory_t */
typedef struct _shape_factory_t {
    brush_factory_t brush_factory;
}shape_factory_t;

static shape_t* brush_shape_create(char* shape) {
    if (!strcmp(shape, "square")) {
        return square_create();
    } else if (!strcmp(shape, "circle")) {
        return circle_create();
    }
    return NULL;
}

static void brush_shape_destroy(shape_t* shape) {
    free(shape);
}

shape_factory_t* shape_factory_create() {
    shape_factory_t* shape_factory = malloc(sizeof(shape_factory_t));
    shape_factory->brush_factory.brush_shape_create = brush_shape_create;
    shape_factory->brush_factory.brush_shape_destory = brush_shape_destroy;
    shape_factory->brush_factory.brush_color_create = NULL;
    shape_factory->brush_factory.brush_color_destroy = NULL;
    return shape_factory;
}

/* color_factory_t implement brush_factory_t */
typedef struct _color_factory_t {
    brush_factory_t brush_factory;
}color_factory_t;

static color_t* brush_color_create(char* color) {
    if (!strcmp(color, "red")) {
        return red_create();
    } else if (!strcmp(color, "blue")) {
        return blue_create();
    }
    return NULL;
}

static void brush_color_destroy(color_t* color) {
    free(color);
}

color_factory_t* color_factory_create() {
    color_factory_t* color_factory = malloc(sizeof(color_factory_t));
    color_factory->brush_factory.brush_shape_create = NULL;
    color_factory->brush_factory.brush_shape_destory = NULL;
    color_factory->brush_factory.brush_color_create = brush_color_create;
    color_factory->brush_factory.brush_color_destroy = brush_color_destroy;
    return color_factory;
}

5、实现工厂生成器( factory_producer_t ),提供给用户使用:

/************************* factory_producer_t  *************************/
brush_factory_t* factory_create(char* choice) {
    if (!strcmp(choice, "shape")) {
        return shape_factory_create();
    }
    else if (!strcmp(choice, "color")) {
        return color_factory_create();
    }
    return NULL;
}

void factory_destroy(brush_factory_t* brush_factory) {
    free(brush_factory);
}

6、使用代码如下:

int main(int argc, const char* argv[]) {
    brush_factory_t* shape_factory = factory_create("shape");
    shape_t* shape1 = shape_factory->brush_shape_create("circle");
    shape_t* shape2 = shape_factory->brush_shape_create("square");
    shape1->draw();
    shape2->draw();

    brush_factory_t* color_factory = factory_create("color");
    color_t* color1 = color_factory->brush_color_create("red");
    color_t* color2 = color_factory->brush_color_create("blue");
    color1->fill();
    color2->fill();
    
    shape_factory->brush_shape_destory(shape1);
    shape_factory->brush_shape_destory(shape2);
    color_factory->brush_color_destroy(color1);
    color_factory->brush_color_destroy(color2);
    factory_destroy(shape_factory);
    factory_destroy(color_factory);
    getchar();

    return 0;
}

7、输出效果:

在这里插入图片描述

四、总结

4.1 抽象工厂模式的优缺点

  • 它分离了具体的类:用户只使用抽象工厂以及抽象产品的接口,具体工厂和具体产品的类不会出现在用户代码中。
  • 它有利于切换产品系列(具体工厂):具体工厂类只在初始化的时候出现一次,那么切换产品系列,只需要修改具体工厂。
  • 它有利于产品的一致性:当一个系列中的产品对象被设计成一起工作时,一个应用一次只能使用同一个系列中的对象。
  • 难以支持新种类的产品(扩展性较差):新增新种类的产品,需要修改抽象工厂类及其所有子类,工作量较大。

4.2 抽象工厂模式的适用场景

  1. 一个系统要独立于它的产品的创建、组合和表示,即要将具体产品类分离出来;
  2. 一个系统要有多个产品系列中的一个来配置,即系统有多个产品系列,但只使用一个产品系列;
  3. 要强调一系列相关产品对象的设计以便进行联合使用,即保证具体产品的一致性;
  4. 提供一个产品类库,但只想显示它们的接口而不是实现。

4.3 简单工厂、抽象工厂和工厂方法之间的区别

最后,通过对抽象工厂模式的学习发现该模式包含了简单工厂模式和工厂方法的某些特性,三者之间的区别如下:

简单工厂模式:一个具体工厂可生产多种具体产品;

抽象工厂模式:一个抽象工厂可生产多种具体工厂,其中每个具体工厂可以生成多种具体产品;

工厂方法模式:一个具体工厂只能生成一种具体产品。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容