C语言工厂方法模式,工厂函数模式 (C语言实现)
工廠模式屬于創建型模式,大致可以分為三類,簡單工廠模式、工廠方法模式、抽象工廠模式。
二. 工廠方法模式
所謂工廠方法模式,是指定義一個用于創建對象的接口,讓子類決定實例化哪一個類。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
總結
以上是生活随笔為你收集整理的C语言工厂方法模式,工厂函数模式 (C语言实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: macos下载的安装包在哪里_macbo
- 下一篇: 从FTP拷贝文件报错无法从指定的位置下载