工厂函数模式 (C语言实现)

来源:互联网 发布:辨别声音属性软件 编辑:程序博客网 时间:2024/04/28 22:02

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

二. 工厂方法模式

所谓工厂方法模式,是指定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。

还以刚才的例子解释,这家电子厂赚了不少钱,于是决定再开设一个厂,其中一个工厂专门用来生产A型号的产品,也就是只生产电吹风,而另一工厂专门用来生产B型号的产品,也就是只生产电风扇,这样分工明确了。以后客户要再下定单时,可以直接找到相关的工厂了,比如要A型号的产品,就找A工厂要,不再担心下的定单是A,生产出来的是B产品了。


代码实现:


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

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);} Factory;#endif

factoryA.h

#ifndef FACTORYA_H#define FACTORYA_Htypedef struct {    const void *_;} _FactoryA;extern const void *FactoryA;#endif

factoryA.c

#include "factory.h"#include "factoryA.h"#include "productA.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 const Factory _factory = {    sizeof(_FactoryA),    factoryACtor,    factoryADtor,    factoryACreateProduct};const void *FactoryA = &_factory;

factoryB.h

#ifndef FACTORYB_H#define FACTORYB_Htypedef struct {    const void *_;} _FactoryB;extern const void *FactoryB;#endif

factoryB.c

#include "factory.h"#include "factoryB.h"#include "productB.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 const Factory _factory = {    sizeof(_FactoryB),    factoryBCtor,    factoryBDtor,    factoryBCreateProduct};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_Htypedef 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_Htypedef 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_Hvoid *New(const void *_class, ...);void Delete(void *_class);void *CreateProduct(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 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);    Delete(facA);    Delete(facB);    Delete(pro1);    Delete(pro2);    Delete(pro3);        return 0;}


图片来源:http://blog.csdn.net/hmsiwtv/article/details/9627109


0 0
原创粉丝点击