【说明】简单工厂模式的C语言实现,改写自http://blog.csdn.net/sx_wpc/article/details/7645062一文的代码。
【代码清单】
typedef.h
#ifndef __TYPEDEF_H__
#define __TYPEDEF_H__
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum _Ret
{
RET_OK,
RET_FAIL
}Ret;
#define return_if_fail(p)\
if(!(p)){\
printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\
return;}
#define return_val_if_fail(p, ret)\
if(!(p)){\
printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\
return (ret);}
#define SAFE_FREE(p) if(p != NULL){free(p); p = NULL;}
#ifdef __cplusplus
}
#endif
#endif
product.h
#ifndef __PRODUCT_H__
#define __PRODUCT_H__
#include <assert.h>
#include "typedef.h"
#ifdef __cplusplus
extern "C" {
#endif
struct _Operation;
typedef struct _Operation Operation;
struct _Result;
typedef struct _Result Result;
typedef Result* (*ResultFunc)(int a, int b);
typedef void (*DestroyFunc)(Operation* thiz);
struct _Result
{
int val;
};
struct _Operation
{
ResultFunc result;
DestroyFunc destroy;
};
static inline Result* oper_result(Operation *thiz, int a, int b)
{
return_val_if_fail(thiz != NULL, NULL);
if(thiz->result != NULL)
{
return thiz->result(a, b);
}
}
static inline void oper_destroy(Operation *thiz)
{
if(thiz != NULL && thiz->destroy != NULL)
{
thiz->destroy(thiz);
}
return ;
}
#ifdef __cplusplus
}
#endif
#endif
producta.h
#ifndef __PRODUCTA_H__
#define __PRODUCTA_H__
#include "product.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _Add Add;
typedef Result* (*AddResultFunc)(int a, int b);
typedef void (*AddDestroyFunc)(Add* thiz);
struct _Add
{
AddResultFunc add_result;
AddDestroyFunc add_destroy;
};
Add *oper_add_create(void);
#ifdef __cplusplus
}
#endif
#endif
producta.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "producta.h"
#include "typedef.h"
static Result *oper_add_result(int a, int b)
{
Result *result = malloc(sizeof(Result));
if(result != NULL)
{
result->val = a + b;
}
return result;
}
static void oper_add_destroy(Add *thiz)
{
if(thiz != NULL)
{
SAFE_FREE(thiz);
}
return ;
}
Add *oper_add_create(void)
{
Add *thiz = malloc(sizeof(Add));
if(thiz != NULL)
{
thiz->add_result = oper_add_result;
thiz->add_destroy = oper_add_destroy;
}
return thiz;
}
productb.h
#ifndef __PRODUCTB_H__
#define __PRODUCTB_H__
#include "product.h"
#ifdef __cplusplus
extern "C" {
#endif
struct _Minus;
typedef struct _Minus Minus;
typedef Result* (*MinusResultFunc)(int a, int b);
typedef void (*MinusDestroyFunc)(Minus* thiz);
struct _Minus
{
MinusResultFunc minus_result;
MinusDestroyFunc minus_destroy;
};
Minus *oper_minus_create(void);
#ifdef __cplusplus
}
#endif
#endif
productb.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "productb.h"
#include "typedef.h"
static Result *oper_minus_result(int a, int b)
{
Result *result = malloc(sizeof(Result));
if(result != NULL)
{
result->val = a - b;
}
return result;
}
static void oper_minus_destroy(Minus *thiz)
{
if(thiz != NULL)
{
SAFE_FREE(thiz);
}
}
Minus *oper_minus_create(void)
{
Minus *thiz = malloc(sizeof(Minus));
if(thiz != NULL)
{
thiz->minus_result = oper_minus_result;
thiz->minus_destroy = oper_minus_destroy;
}
return thiz;
}
factory.h
#ifndef __FACTORY_H__
#define __FACTORY_H__
#include "product.h"
#ifdef __cplusplus
extern "C" {
#endif
struct _Factory;
typedef struct _Factory Factory;
typedef Operation* (*CreateOperFunc)(char operName);
typedef void (*FactoryDestroyFunc)(Factory *thiz);
struct _Factory
{
CreateOperFunc create_oper;
FactoryDestroyFunc destroy;
};
Factory *FactoryCreate(void);
Operation* factory_create_oper(char operName);
void factory_destroy(Factory *thiz);
#ifdef __cplusplus
}
#endif
#endif
factory.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "factory.h"
#include "producta.h"
#include "productb.h"
Operation* factory_create_oper(char operName)
{
Operation *thiz = malloc(sizeof(Operation));
if(thiz != NULL)
{
switch(operName)
{
case '+':
thiz = (Operation *)oper_add_create();
break;
case '-':
thiz = (Operation *)oper_minus_create();
break;
default:;
}
}
return thiz;
}
void factory_destroy(Factory *thiz)
{
if(thiz != NULL)
{
SAFE_FREE(thiz);
}
return ;
}
Factory *FactoryCreate(void)
{
Factory *thiz = malloc(sizeof(Factory));
if(thiz != NULL)
{
thiz->create_oper = factory_create_oper;
thiz->destroy = factory_destroy;
}
}
test.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "factory.h"
#include "product.h"
int main(int argc, char **argv)
{
Factory *factory = FactoryCreate();
Operation *oper = factory_create_oper('+');
Result* result = oper_result(oper, 3, 5);
printf("%d\n", result->val);
oper_destroy(oper);
factory->destroy(factory);
return 0;
}
Makefile
exe:=test
temp := $(wildcard test *~)
all:test.c factory.c factory.h product.h producta.c producta.h productb.c productb.h typedef.h
gcc -g $^ -o $(exe)
clean:
rm $(temp)
【源码下载】
*欢迎纠错,共同进步!
转载请标明出处,仅供学习交流,勿用于商业目的
Copyright @ http://blog.csdn.net/tandesir