工厂模式属于创建型模式,大致可以分为三类,简单工厂模式、工厂方法模式、抽象工厂模式。
一. 简单工厂模式
简单工厂模式,它的主要特点是需要在工厂类中做判断,从而创造相应的产品。当增加新的产品时,就需要修改工厂类。举个例子,有一家电子产品生产厂家,它只有一个工厂,能够生产两种型号的产品,A 和 B。可以想像一下,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 enum {
PRODUCT_A,
PRODUCT_B
} PRODUCT_TYPE;
typedef struct {
size_t size;
void* (*ctor)(void *_self, va_list *params);
void* (*dtor)(void *_self);
void* (*createProduct)(const void *_self, PRODUCT_TYPE productType);
} _Factory;
extern const void *Factory;
#endif
factory.c
#include "new.h"
#include "factory.h"
#include "productA.h"
#include "productB.h"
#include <stdlib.h>
#include <stdarg.h>
static void *factoryCtor(void *_self, va_list *params) {
_Factory *self = _self;
return self;
}
static void *factoryDtor(void *_self) {
_Factory *self = _self;
return self;
}
static void* factoryCreateProduct(const void *_self, PRODUCT_TYPE productType) {
void *product = NULL;
switch(productType) {
case PRODUCT_A:
product = New(ProductA);
break;
case PRODUCT_B:
product = New(ProductB);
break;
default:
break;
}
return product;
}
static const _Factory _factory = {
sizeof(_Factory),
factoryCtor,
factoryDtor,
factoryCreateProduct
};
const void *Factory = &_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, int productType);
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, int productType) {
return ((const _Factory*)Factory)->createProduct(_factory, productType);
}
void Show(const void *_product) {
const Product * const *product = _product;
if (_product && *product && (*product)->show) {
(*product)->show(_product);
}
}
main.c
#include "new.h"
#include "factory.h"
int main(int argc, char *argv[]) {
void *fac = New(Factory);
void *pro1 = CreateProduct(fac, PRODUCT_A);
void *pro2 = CreateProduct(fac, PRODUCT_B);
Show(pro1);
Show(pro2);
Delete(fac);
Delete(pro1);
Delete(pro2);
return 0;
}
图片来源:http://blog.csdn.net/hmsiwtv/article/details/9627109