使用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}









