工厂模式属于创建型模式,大致可以分为三类,简单工厂模式、工厂方法模式、抽象工厂模式。
二. 工厂方法模式
所谓工厂方法模式,是指定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
还以刚才的例子解释,这家电子厂赚了不少钱,于是决定再开设一个厂,其中一个工厂专门用来生产A型号的产品,也就是只生产电吹风,而另一工厂专门用来生产B型号的产品,也就是只生产电风扇,这样分工明确了。以后客户要再下定单时,可以直接找到相关的工厂了,比如要A型号的产品,就找A工厂要,不再担心下的定单是A,生产出来的是B产品了。
代码实现:
abstractClass.h
#ifndef ABSTRACTCLASS_H
#define ABSTRACTCLASS_H
#include
#include
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
#include
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_H
typedef struct {
const void *_;
} _FactoryA;
extern const void *FactoryA;
#endif
factoryA.c
#include "factory.h"
#include "factoryA.h"
#include "productA.h"
#include "new.h"
#include
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_H
typedef struct {
const void *_;
} _FactoryB;
extern const void *FactoryB;
#endif
factoryB.c
#include "factory.h"
#include "factoryB.h"
#include "productB.h"
#include "new.h"
#include
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
#include
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
#include
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
#include
#include
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 Show(const void *product);
#endif
new.c
#include "new.h"
#include "abstractClass.h"
#include "factory.h"
#include "product.h"
#include
#include
#include
#include
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