工厂模式是软件设计中经常使用到的设计模式之一。
 使用工厂模式时,在创建对象的过程中,不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
 使用该模式的好处是,可以在不修改原有代码的基础上加入新的产品,满足软件设计的开闭原则。
优点
- 使用者在创建对象时,只需要知道该对象的名称即可。
- 代码扩展性强,如果想要增加一个新产品,只需要再增加一个类即可。
- 使代码得到解耦。
缺点
- 产品增多时,对应的类将会变多,增加了系统的复杂度。
- 增加了系统的抽象性,使之不好理解
应用场景
- 一个系统要独立于它的产品的创建、组合和表示,即要将具体产品类分离出来。
- 一个系统要有多个产品系列中的一个来配置,即系统有多个产品系列,但只使用一个产品系列。
- 提供一个产品类库,但只想显示它们的接口而不是实现。
实现
-  代码在文末下载 
  
-  创建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
关注v-x-公-众-号:【嵌入式基地】
 后-台-回-复:【电赛】 即可获资料
 回复【编程】即可获取
 包括有:C、C++、C#、JAVA、Python、JavaScript、PHP、数据库、微信小程序、人工智能、嵌入式、Linux、Unix、QT、物联网、算法导论、大数据等资料











