国产性生交xxxxx免费-国产中文字幕-啊灬啊灬啊灬快灬高潮了,亚洲国产午夜精品理论片在线播放 ,亚洲欧洲日本无在线码,色爽交视频免费观看

鍋爐信息網 > 鍋爐知識 > 鍋爐資訊

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

上一篇:加油站

下一篇:安岷老師簡介

精選推薦

  • 如何正確選擇白板供應商
    如何正確選擇白板供應商

    目前在無錫想采購一塊白板不管是實體店鋪,還是網絡平臺都有很多選擇,想要到專業的無錫白板公司采購還需要掌握一定的方式技巧。現

  • 柴油發電機組供應商
    柴油發電機組供應商

      t 揚州華東動力機械有限公司,位于江蘇省揚州市江都區仙城工業園,是專業從事發電機、柴油及燃氣發電機組研發、制造、銷售、服務于

  • 高溫輻射爐
    高溫輻射爐

    5.2.2高溫輻射爐5.2.2.1溫度控制★(1)樣品溫度范圍:常溫~1400℃。★(2)均溫區:長度不小于80mm。★(3)中心區:長度不小于10mm。(4)溫度梯度(均

  • 高壓鍋在什么情況下會爆炸?
    高壓鍋在什么情況下會爆炸?

    近日,多地發生高壓鍋爆炸事故,給不少家庭帶來了傷害和財產損失。那么,什么情況下會導致高壓鍋爆炸呢?首先,當高壓鍋內部壓力過高時,如果

0