工厂模式属于创建型模式,大致可以分为三类,简单工厂模式、工厂方法模式、抽象工厂模式。
三. 抽象工厂模式
抽象工厂模式,它定义为提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
还以上面的例子解释,这家公司经营的还不错,针对其核心技术都申请了专利,后来生始生产相关电子芯片了 (像三星一样,即生产产品也生产芯片)。对于这种情况,我们不能在抽象产品类上继承一个零部件,因为产品和芯片没有什么共同之处,完全是两个领域。你要做芯片,就要专心做,当成一个新的领域,不能用以前产品的那一套流程。这样划分才明确,才能更好的实现高内聚,低耦合的明标。
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;
}