抽象工厂模式 (C语言实现)

工厂模式属于创建型模式,大致可以分为三类,简单工厂模式、工厂方法模式、抽象工厂模式。


三. 抽象工厂模式

抽象工厂模式,它定义为提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

还以上面的例子解释,这家公司经营的还不错,针对其核心技术都申请了专利,后来生始生产相关电子芯片了 (像三星一样,即生产产品也生产芯片)。对于这种情况,我们不能在抽象产品类上继承一个零部件,因为产品和芯片没有什么共同之处,完全是两个领域。你要做芯片,就要专心做,当成一个新的领域,不能用以前产品的那一套流程。这样划分才明确,才能更好的实现高内聚,低耦合的明标。

abstractClass.h

#ifndef ABSTRACTCLASS_H
#define ABSTRACTCLASS_H

#include <stdlib.h>
#include <stdarg.h>

typedef struct {
    size_t size;
    void* (*ctor)(void *_self, va_list *params);
    void* (*dtor)(void *_self);
} AbstractClass;

#endif

chip.h

#ifndef CHIP_H
#define CHIP_H

#include <stdlib.h>
#include <stdarg.h>

typedef struct {
    size_t size;
    void* (*ctor)(void *_self, va_list *params);
    void* (*dtor)(void *_self);
    void (*show)(const void *_self);
} Chip;

#endif

chipA.h

#ifndef CHIPA_H
#define CHIPA_H

typedef struct {
    const void *_;
} _ChipA;

extern const void *ChipA;

#endif

chipA.c

#include "chip.h"
#include "chipA.h"
#include <stdarg.h>
#include <stdio.h>

static void *chipACtor(void *_self, va_list *params) {
    _ChipA *self = _self;

    return self;
}

static void *chipADtor(void *_self) {
    _ChipA *self = _self;

    return self;
}

static void chipAShow(const void *_self) {
    (void)_self;
    fprintf(stdout, "Chip A\n");
}

static const Chip _chip = {
    sizeof(_ChipA),
    chipACtor,
    chipADtor,
    chipAShow
};

const void *ChipA = &_chip;

chipB.h

#ifndef CHIPB_H
#define CHIPB_H

typedef struct {
    const void *_;
} _ChipB;

extern const void *ChipB;

#endif

chipB.c

#include "chip.h"
#include "chipB.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

static void *chipBCtor(void *_self, va_list *params) {
    _ChipB *self = _self;

    return self;
}

static void *chipBDtor(void *_self) {
    _ChipB *self = _self;

    return self;
}

static void chipBShow(const void *_self) {
    (void)_self;
    fprintf(stdout, "Chip B\n");
}

static const Chip _chip = {
    sizeof(_ChipB),
    chipBCtor,
    chipBDtor,    
    chipBShow
};

const void *ChipB = &_chip;

factory.h

#ifndef FACTORY_H
#define FACTORY_H

#include <stdlib.h>
#include <stdarg.h>

typedef struct {
    size_t size;
    void* (*ctor)(void *_self, va_list *params);
    void* (*dtor)(void *_self);
    void* (*createProduct)(const void *_self);
    void* (*createChip)(const void *_self);
} Factory;

#endif

factoryA.h

#ifndef FACTORYA_H
#define FACTORYA_H

typedef struct {
    const void *_;
} _FactoryA;

extern const void *FactoryA;


#endif

factoryA.c

#include "factory.h"
#include "factoryA.h"
#include "productA.h"
#include "chipA.h"
#include "new.h"
#include <stdarg.h>

static void *factoryACtor(void *_self, va_list *params){
    _FactoryA *self = _self;

    return self;
}

static void *factoryADtor(void *_self) {
    _FactoryA *self = _self;

    return self;
}

static void* factoryACreateProduct(const void *self) {
    return New(ProductA);
}

static void* factoryACreateChip(const void *self) {
    return New(ChipA);
}

static const Factory _factory = {
    sizeof(_FactoryA),
    factoryACtor,
    factoryADtor,
    factoryACreateProduct,
    factoryACreateChip
};

const void *FactoryA = &_factory;

factoryB.h

#ifndef FACTORYB_H
#define FACTORYB_H

typedef struct {
    const void *_;
} _FactoryB;

extern const void *FactoryB;

#endif

factoryB.c

#include "factory.h"
#include "factoryB.h"
#include "productB.h"
#include "chipB.h"
#include "new.h"
#include <stdarg.h>

static void *factoryBCtor(void *_self, va_list *params){
    _FactoryB *self = _self;

    return self;
}

static void *factoryBDtor(void *_self) {
    _FactoryB *self = _self;

    return self;
}

static void* factoryBCreateProduct(const void *self) {
    return New(ProductB);
}

static void* factoryBCreateChip(const void *self) {
    return New(ChipB);
}

static const Factory _factory = {
    sizeof(_FactoryB),
    factoryBCtor,
    factoryBDtor,
    factoryBCreateProduct,
    factoryBCreateChip
};

const void *FactoryB = &_factory;

product.h

#ifndef PRODUCT_H
#define PRODUCT_H

#include <stdlib.h>
#include <stdarg.h>

typedef struct {
    size_t size;
    void* (*ctor)(void *_self, va_list *params);
    void* (*dtor)(void *_self);
    void (*show)(const void *_self);
} Product;

#endif

productA.h

#ifndef PRODUCTA_H
#define PRODUCTA_H

typedef struct {
    const void *_;
} _ProductA;

extern const void *ProductA;

#endif

productA.c

#include "product.h"
#include "productA.h"
#include <stdarg.h>
#include <stdio.h>

static void *productACtor(void *_self, va_list *params) {
    _ProductA *self = _self;

    return self;
}

static void *productADtor(void *_self) {
    _ProductA *self = _self;

    return self;
}

static void productAShow(const void *_self) {
    (void)_self;
    fprintf(stdout, "Product A\n");
}

static const Product _product = {
    sizeof(_ProductA),
    productACtor,
    productADtor,
    productAShow
};

const void *ProductA = &_product;

productB.h

#ifndef PRODUCTB_H
#define PRODUCTB_H

typedef struct {
    const void *_;
} _ProductB;

extern const void *ProductB;

#endif

productB.c

#include "product.h"
#include "productB.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

static void *productBCtor(void *_self, va_list *params) {
    _ProductB *self = _self;

    return self;
}

static void *productBDtor(void *_self) {
    _ProductB *self = _self;

    return self;
}

static void productBShow(const void *_self) {
    (void)_self;
    fprintf(stdout, "Product B\n");
}

static const Product _product = {
    sizeof(_ProductB),
    productBCtor,
    productBDtor,    
    productBShow
};

const void *ProductB = &_product;

new.h

#ifndef NEW_H
#define NEW_H

void *New(const void *_class, ...);
void Delete(void *_class);
void *CreateProduct(const void *_factory);
void *CreateChip(const void *_factory);
void Show(const void *product);

#endif

new.c

#include "new.h"
#include "abstractClass.h"
#include "factory.h"
#include "product.h"
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

void *New(const void *_class, ...) {
    const AbstractClass *class = _class;

    void *p = calloc(1, class->size);

    assert(p);
    *(const AbstractClass **)p = class;
    
    if (class->ctor) {
        va_list params;

        va_start(params, _class);
        p = class->ctor(p, ¶ms);
        va_end(params);
    }

    return p;
}

void Delete(void *_class) {
    const AbstractClass **class = _class;

    if (_class && *class && (*class)->dtor) {
        _class = (*class)->dtor(_class);
    }

    free(_class);
}

void *CreateProduct(const void *_factory) {
    const Factory * const *factory = _factory;
    if (_factory && *factory && (*factory)->createProduct) {
        return (*factory)->createProduct(_factory);
    } else {
        return NULL;
    }
}

void *CreateChip(const void *_factory) {
    const Factory * const *factory = _factory;
    if (_factory && *factory && (*factory)->createChip) {
        return (*factory)->createChip(_factory);
    } else {
        return NULL;
    }    
}

void Show(const void *_product) {
    const Product * const *product = _product;
    if (_product && *product && (*product)->show) {    
        (*product)->show(_product);
    }
}

main.c

#include "new.h"
#include "factoryA.h"
#include "factoryB.h"

int main(int argc, char *argv[]) {
    void *facA = New(FactoryA);
    void *facB = New(FactoryB);

    void *pro1 = CreateProduct(facA);
    void *pro2 = CreateProduct(facB);
    void *pro3 = CreateProduct(facA);

    Show(pro1);
    Show(pro2);
    Show(pro3);

    void *pro4 = CreateChip(facA);
    void *pro5 = CreateChip(facB);
    void *pro6 = CreateChip(facA);

    Show(pro4);
    Show(pro5);
    Show(pro6);
    
    Delete(facA);
    Delete(facB);
    Delete(pro1);
    Delete(pro2);
    Delete(pro3);
    Delete(pro4);
    Delete(pro5);
    Delete(pro6);

    
    return 0;
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抽象工厂模式是一种创建型设计模式,它允许客户端代码使用抽象接口来创建一组相关或依赖对象,而无需指定其具体类。 在C语言中,可以使用函数指针来模拟抽象类和虚函数,从而实现抽象工厂模式。 下面是一个简单的示例,演示如何使用C语言实现抽象工厂模式: ```c #include <stdio.h> // 定义抽象产品1接口 typedef struct { void (*operation1)(void); } AbstractProduct1; // 定义抽象产品2接口 typedef struct { void (*operation2)(void); } AbstractProduct2; // 定义抽象工厂接口 typedef struct { AbstractProduct1* (*createProduct1)(void); AbstractProduct2* (*createProduct2)(void); } AbstractFactory; // 定义具体产品1A typedef struct { AbstractProduct1 base; } Product1A; // 实现具体产品1A的操作1 static void operation1A(void) { printf("Product 1A Operation 1\n"); } // 定义具体产品2A typedef struct { AbstractProduct2 base; } Product2A; // 实现具体产品2A的操作2 static void operation2A(void) { printf("Product 2A Operation 2\n"); } // 定义具体工厂A typedef struct { AbstractFactory base; } FactoryA; // 实现具体工厂A的createProduct1函数 static AbstractProduct1* createProduct1A(void) { Product1A* product = malloc(sizeof(Product1A)); product->base.operation1 = operation1A; return &product->base; } // 实现具体工厂A的createProduct2函数 static AbstractProduct2* createProduct2A(void) { Product2A* product = malloc(sizeof(Product2A)); product->base.operation2 = operation2A; return &product->base; } // 定义具体产品1B typedef struct { AbstractProduct1 base; } Product1B; // 实现具体产品1B的操作1 static void operation1B(void) { printf("Product 1B Operation 1\n"); } // 定义具体产品2B typedef struct { AbstractProduct2 base; } Product2B; // 实现具体产品2B的操作2 static void operation2B(void) { printf("Product 2B Operation 2\n"); } // 定义具体工厂B typedef struct { AbstractFactory base; } FactoryB; // 实现具体工厂B的createProduct1函数 static AbstractProduct1* createProduct1B(void) { Product1B* product = malloc(sizeof(Product1B)); product->base.operation1 = operation1B; return &product->base; } // 实现具体工厂B的createProduct2函数 static AbstractProduct2* createProduct2B(void) { Product2B* product = malloc(sizeof(Product2B)); product->base.operation2 = operation2B; return &product->base; } int main() { // 创建具体工厂A FactoryA factoryA; factoryA.base.createProduct1 = createProduct1A; factoryA.base.createProduct2 = createProduct2A; // 使用具体工厂A创建产品 AbstractProduct1* product1A = factoryA.base.createProduct1(); AbstractProduct2* product2A = factoryA.base.createProduct2(); // 调用产品的操作 product1A->operation1(); product2A->operation2(); // 创建具体工厂B FactoryB factoryB; factoryB.base.createProduct1 = createProduct1B; factoryB.base.createProduct2 = createProduct2B; // 使用具体工厂B创建产品 AbstractProduct1* product1B = factoryB.base.createProduct1(); AbstractProduct2* product2B = factoryB.base.createProduct2(); // 调用产品的操作 product1B->operation1(); product2B->operation2(); return 0; } ``` 在上面的示例中,我们定义了抽象产品1和抽象产品2的接口,以及抽象工厂的接口。然后使用具体的产品和工厂来实现这些接口。最后,我们可以使用具体工厂来创建具体产品,并调用它们的操作。 需要注意的是,在C语言中,需要手动管理内存,因此需要在使用完产品后释放它们的内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值