锅炉信息网 > 锅炉知识 > 锅炉资讯

使用C语言实现工厂模式

工厂模式是软件设计中经常使用到的设计模式之一。使用工厂模式时,在创建对象的过程中,不会对客户端暴露创建逻辑,并且是通过使用一个

工厂模式是软件设计中经常使用到的设计模式之一。
使用工厂模式时,在创建对象的过程中,不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
使用该模式的好处是,可以在不修改原有代码的基础上加入新的产品,满足软件设计的开闭原则。

优点

  • 使用者在创建对象时,只需要知道该对象的名称即可。
  • 代码扩展性强,如果想要增加一个新产品,只需要再增加一个类即可。
  • 使代码得到解耦。

缺点

  • 产品增多时,对应的类将会变多,增加了系统的复杂度。
  • 增加了系统的抽象性,使之不好理解

应用场景

  • 一个系统要独立于它的产品的创建、组合和表示,即要将具体产品类分离出来。
  • 一个系统要有多个产品系列中的一个来配置,即系统有多个产品系列,但只使用一个产品系列。
  • 提供一个产品类库,但只想显示它们的接口而不是实现。

实现

  • 代码仓库:https://github.com/Lighter-z/DesignPattern


  • 创建Shape接口并实现

typedef struct Shape Shape;nnstruct Shape {n void *priv_;n void (*Draw)(struct Shape *c_this);n void (*Destroy)(struct Shape *c_this);n};nnvoid ShapeDraw(Shape *c_this);nvoid ShapeDestory(Shape **c_this);n


void ShapeDraw(Shape *c_this) {n assert(c_this != NULL);n if(c_this->Draw != NULL) {n c_this->Draw(c_this);n }n}nvoid ShapeDestory(Shape **c_this) {n if(c_this == NULL || *c_this == NULL) {n return;n }n Shape *shape = *c_this;n if(shape->Destroy != NULL) {n shape->Destroy(shape);n }n free(*c_this);n *c_this = NULL;n}

  • 创建并实现工厂类ShapeFactory

//ShapeFactory.h

#include "shape.h"nShape* ShapeFactoryCreateShape(const char *shape_type);

//ShapeFactory.c

extern struct Shape* CircleCreate(void);nextern struct Shape* RectangleCreate(void);nextern struct Shape* SquareCreate(void);nnShape* ShapeFactoryCreateShape(const char *shape_type) {n if(shape_type == NULL) {n return NULL;n }n if (0 == strcasecmp("CIRCLE", shape_type)) {n return CircleCreate();n } else if (0 == strcasecmp("RECTANGLE", shape_type)) {n return RectangleCreate();n } else if (0 == strcasecmp("SQUARE", shape_type)) {n return SquareCreate();n } else {n return NULL;n }n}

  • 创建实现接口的实体类

//1.Circle类nstatic void CircleDraw(struct Shape *c_this) {n printf("Circle draw method.n");n}nnstruct Shape *CircleCreate(void) {n struct Shape *c_this = (struct Shape *)malloc(sizeof(struct Shape));nn if(c_this == NULL) {n return NULL;;n }n memset(c_this, 0, sizeof(struct Shape));n c_this->Draw = CircleDraw;n return c_this;n}n//2.Rectangle类nstatic void RectangleDraw(struct Shape *c_this) {n printf("Rectangle draw method.n");n}nnstruct Shape *RectangleCreate(void) {n struct Shape *c_this = (struct Shape *)malloc(sizeof(struct Shape));nn if(c_this == NULL) {n return NULL;;n }n memset(c_this, 0, sizeof(struct Shape));n c_this->Draw = RectangleDraw;n return c_this;n}n//3.Square类nstatic void SquareDraw(struct Shape *c_this) {n printf("Square draw method.n");n}nnstruct Shape *SquareCreate(void) {n struct Shape *c_this = (struct Shape *)malloc(sizeof(struct Shape));nn if(c_this == NULL) {n return NULL;;n }n memset(c_this, 0, sizeof(struct Shape));n c_this->Draw = SquareDraw;n return c_this;n}

  • FactoryPatternDemo类使用ShapeFactory来获取Shape对象

void main(void) {n //获取 Circle 的对象,并调用它的 draw 方法n Shape* circle_shape = ShapeFactoryCreateShape("CIRCLE");n ShapeDraw(circle_shape);n ShapeDestory(&circle_shape);nn //获取 Rectangle 的对象,并调用它的 draw 方法n Shape* rectangle_shape = ShapeFactoryCreateShape("RECTANGLE");n ShapeDraw(rectangle_shape);n ShapeDestory(&rectangle_shape);nn //获取 Square 的对象,并调用它的 draw 方法n Shape* square_shape = ShapeFactoryCreateShape("SQUARE");n ShapeDraw(square_shape);n ShapeDestory(&square_shape);n system("pause");n}

上一篇:加油站

下一篇:安岷老师简介

锅炉资讯

锅炉资讯

锅炉学习

锅炉学习

锅炉视频

锅炉视频

锅炉百科

锅炉百科