C语言-工厂模式

1.工厂模式介绍

  • 使用工厂模式时,在创建对象的过程中,不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

2.工厂模式优点

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

3.工厂模式缺点

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

4.代码示例

点击查看代码
//创建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

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");
}






posted @   Charles_hui  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示