ファクトリ モード -- C 言語の実装

ファクトリ モードは、実際には C++ のクラスの継承ですが、C 言語では関数ポインターで実装されます。

現在の需要が、会社が椅子やベッドなどの一連の製品を生産することであると仮定すると、最初に製品を生産し、次に生産された製品を販売する必要があります。将来的に新しい製品が追加される可能性がありますが、オープン/クローズの原則を満たすために既存のコア コード フローを変更することは想定されていません。

/*
    工厂模式,其实就是C++中的类的继承,只不过用C语言中的函数指针来实现了
*/

/*
    假设现在的需求是公司生产一系列产品,包括椅子,床等,要求先生产产品,然后将
    生产的产品销售出去。
    将来可能要增加新的产品,不希望修改现有的核心代码流程,满足开闭原则
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct PRO_STR
{
    void (*produce)(struct PRO_STR *pthis);
    void (*sell)(struct PRO_STR **pthis);
} PRO_STR, *pPRO_STR;


// 实现两个接口
void produceProduct(PRO_STR *pthis) {
    if (pthis == NULL) return;
    pthis->produce(pthis);
}

void sellProduct(PRO_STR **pthis) {
    if (pthis == NULL || *pthis == NULL) return;

    (*pthis)->sell(pthis);
    free(*pthis);
    *pthis = NULL;
}


pPRO_STR createChair();
pPRO_STR createBed();

// 工厂创建接口
pPRO_STR factoryCreate(char *strName) {

    if (0 == strcasecmp("CHAIR", strName)) {
        return createChair();
    }

    if (0 == strcasecmp("BED", strName)) {
        return createBed();
    }
}

// 子类接口实现
void produceChair(PRO_STR *pthis) {
    printf("produce a chair\n");
}

void sellChair(PRO_STR **pthis) {
    printf("sell a chair\n");
}

pPRO_STR createChair() {
    pPRO_STR pthis = (pPRO_STR)malloc(sizeof(PRO_STR));
    if (pthis == NULL) return NULL;

    pthis->produce = produceChair;
    pthis->sell = sellChair;

    return pthis;
}

void produceBed(PRO_STR *pthis) {
    printf("produce a bed\n");
}

void sellBed(PRO_STR **pthis) {
    printf("sell a bed\n");
}

pPRO_STR createBed() {
    pPRO_STR pthis = (pPRO_STR)malloc(sizeof(PRO_STR));
    if (pthis == NULL) return NULL;

    pthis->produce = produceBed;
    pthis->sell = sellBed;

    return pthis;
}

int main() {

    pPRO_STR chair = factoryCreate("CHAIR");
    produceProduct(chair);
    sellProduct(&chair);

    pPRO_STR bed = factoryCreate("BED");
    produceProduct(bed);
    sellProduct(&bed);

    return 0;
}

新しい要件、各製品には独自の属性があります。このとき、各製品カテゴリの属性構造を外部から提供する必要があります。

/*
    工厂模式,其实就是C++中的类的继承,只不过用C语言中的函数指针来实现了
*/

/*
    假设现在的需求是公司生产一系列产品,包括椅子,床等,要求先生产产品,然后将
    生产的产品销售出去。
    将来可能要增加新的产品,不希望修改现有的核心代码流程,满足开闭原则
*/

/*
    新的需求,每个产品有自己的属性
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct PRO_STR
{
    void (*produce)(struct PRO_STR *pthis);
    void (*sell)(struct PRO_STR **pthis);
    void (*print)(struct PRO_STR *pthis);
    void *attr;
} PRO_STR, *pPRO_STR;


// 实现两个接口
void produceProduct(PRO_STR *pthis) {
    if (pthis == NULL) return;
    pthis->produce(pthis);
}

void sellProduct(PRO_STR **pthis) {
    if (pthis == NULL || *pthis == NULL) return;

    (*pthis)->sell(pthis);
    free(*pthis);
    *pthis = NULL;
}

void printProduct(PRO_STR *pthis) {
    if (pthis == NULL) return;
    pthis->print(pthis);
}

pPRO_STR createChair(void *attr);
pPRO_STR createBed(void *attr);

// 工厂创建接口
pPRO_STR factoryCreate(char *strName, void *attr) {

    if (0 == strcasecmp("CHAIR", strName)) {
        return createChair(attr);
    }

    if (0 == strcasecmp("BED", strName)) {
        return createBed(attr);
    }
}

// 子类接口实现
typedef struct CHAIR
{
    int heavyAbility;
    int legs;
} CHAIR;

void produceChair(PRO_STR *pthis) {
    printf("produce a chair\n");
}

void sellChair(PRO_STR **pthis) {
    printf("sell a chair\n");
}

void printChair(PRO_STR *pthis) {
    if (NULL == pthis) return;

    CHAIR *pChair = (CHAIR *)(pthis->attr);
    printf("heavy %d legs %d \n", pChair->heavyAbility, pChair->legs);
}

pPRO_STR createChair(void *attr) {
    pPRO_STR pthis = (pPRO_STR)malloc(sizeof(PRO_STR));
    if (pthis == NULL) return NULL;

    pthis->attr = (CHAIR *)malloc(sizeof(CHAIR));
    if (NULL == pthis->attr) return NULL;

    pthis->produce = produceChair;
    pthis->sell = sellChair;
    pthis->print = printChair;
    memcpy(pthis->attr, attr, sizeof(CHAIR));

    return pthis;
}

typedef struct BED
{
    int big;
    int height;
    int lenth;
} BED;

void produceBed(PRO_STR *pthis) {
    printf("produce a bed\n");
}

void sellBed(PRO_STR **pthis) {
    printf("sell a bed\n");
}

void printBed(PRO_STR *pthis) {
    if (NULL == pthis) return;

    BED *pBed = (BED *)(pthis->attr);
    printf("big %d height %d length %d \n", pBed->big, pBed->height, pBed->lenth);
}

pPRO_STR createBed(void *attr) {
    pPRO_STR pthis = (pPRO_STR)malloc(sizeof(PRO_STR));
    if (pthis == NULL) return NULL;

    pthis->attr = (BED *)malloc(sizeof(BED));
    if (NULL == pthis->attr) return NULL;

    pthis->produce = produceBed;
    pthis->sell = sellBed;
    pthis->print = printBed;

    memcpy(pthis->attr, attr, sizeof(BED));

    return pthis;
}

int main() {
    CHAIR chairAttr = {100,4};
    pPRO_STR chair = factoryCreate("CHAIR", &chairAttr);
    produceProduct(chair);
    printProduct(chair);
    sellProduct(&chair);

    BED bedAttr = {10, 10, 7};
    pPRO_STR bed = factoryCreate("BED", &bedAttr);
    produceProduct(bed);
    printProduct(bed);
    sellProduct(&bed);

    return 0;
}

コンパイルして実行する

parallels@ubuntu-linux-20-04-desktop:~/leecode2/marco$ ./a.out 
produce a chair
heavy 100 legs 4 
sell a chair
produce a bed
big 10 height 10 length 7 
sell a bed

おすすめ

転載: blog.csdn.net/daida2008/article/details/125053616