使用C语言实现工厂模式

首先呢,给大家分享一个C语言练习的网站C语言练习

工厂模式是软件设计中经常使用到的设计模式之一。

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

优点
  • 使用者在创建对象时,只需要知道该对象的名称即可。
  • 代码扩展性强,如果想要增加一个新产品,只需要再增加一个类即可。
  • 使代码得到解耦。
缺点
  • 产品增多时,对应的类将会变多,增加了系统的复杂度。
  • 增加了系统的抽象性,使之不好理解
应用场景
  • 一个系统要独立于它的产品的创建、组合和表示,即要将具体产品类分离出来。
  • 一个系统要有多个产品系列中的一个来配置,即系统有多个产品系列,但只使用一个产品系列。
  • 提供一个产品类库,但只想显示它们的接口而不是实现。
实现
  • 代码在文末下载

  • 创建Shape接口并实现

typedef struct Shape Shape;

struct Shape {
  void *priv_;
  void (*Draw)(struct Shape *c_this);
  void (*Destroy)(struct Shape *c_this);
};

void ShapeDraw(Shape *c_this);
void ShapeDestory(Shape **c_this);
void ShapeDraw(Shape *c_this) {
  assert(c_this != NULL);
  if(c_this->Draw != NULL) {
    c_this->Draw(c_this);
  }
}
void ShapeDestory(Shape **c_this) {
  if(c_this == NULL || *c_this == NULL) {
    return;
  }
  Shape *shape = *c_this;
  if(shape->Destroy != NULL) {
    shape->Destroy(shape);
  }
  free(*c_this);
  *c_this = NULL;
}
  • 创建并实现工厂类ShapeFactory
#include "shape.h"
Shape* ShapeFactoryCreateShape(const char *shape_type);
extern struct Shape* CircleCreate(void);
extern struct Shape* RectangleCreate(void);
extern struct Shape* SquareCreate(void);

Shape* ShapeFactoryCreateShape(const char *shape_type) {
  if(shape_type == NULL) {
    return NULL;
  }
  if (0 == strcasecmp("CIRCLE", shape_type)) {
    return CircleCreate();
  } else if (0 == strcasecmp("RECTANGLE", shape_type)) {
    return RectangleCreate();
  } else if (0 == strcasecmp("SQUARE", shape_type)) {
    return SquareCreate();
  } else {
  return NULL;
  }
}
  • 创建实现接口的实体类
//1.Circle类
static void CircleDraw(struct Shape *c_this) {
  printf("Circle draw method.\n");
}

struct Shape *CircleCreate(void) {
  struct Shape *c_this = (struct Shape *)malloc(sizeof(struct Shape));

  if(c_this == NULL) {
    return NULL;;
  }
  memset(c_this, 0, sizeof(struct Shape));
  c_this->Draw = CircleDraw;
  return c_this;
}
//2.Rectangle类
static void RectangleDraw(struct Shape *c_this) {
  printf("Rectangle draw method.\n");
}

struct Shape *RectangleCreate(void) {
  struct Shape *c_this = (struct Shape *)malloc(sizeof(struct Shape));

  if(c_this == NULL) {
    return NULL;;
  }
  memset(c_this, 0, sizeof(struct Shape));
  c_this->Draw = RectangleDraw;
  return c_this;
}
//3.Square类
static void SquareDraw(struct Shape *c_this) {
  printf("Square draw method.\n");
}

struct Shape *SquareCreate(void) {
  struct Shape *c_this = (struct Shape *)malloc(sizeof(struct Shape));

  if(c_this == NULL) {
    return NULL;;
  }
  memset(c_this, 0, sizeof(struct Shape));
  c_this->Draw = SquareDraw;
  return c_this;
}
  • FactoryPatternDemo类使用ShapeFactory来获取Shape对象
void main(void) {
  //获取 Circle 的对象,并调用它的 draw 方法
  Shape* circle_shape = ShapeFactoryCreateShape("CIRCLE");
  ShapeDraw(circle_shape);
  ShapeDestory(&circle_shape);

  //获取 Rectangle 的对象,并调用它的 draw 方法
  Shape* rectangle_shape = ShapeFactoryCreateShape("RECTANGLE");
  ShapeDraw(rectangle_shape);
  ShapeDestory(&rectangle_shape);

  //获取 Square 的对象,并调用它的 draw 方法
  Shape* square_shape = ShapeFactoryCreateShape("SQUARE");
  ShapeDraw(square_shape);
  ShapeDestory(&square_shape);
  system("pause");
}
  • 代码仓库:https://github.com/Lighter-z/DesignPattern
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式基地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值