c语言 标准工厂模式,【代码改写】简略工厂模式C语言实现

【代码改写】简单工厂模式C语言实现

【说明】简单工厂模式的C语言实现,改写自http://blog.csdn.net/sx_wpc/article/details/7645062一文的代码。

【代码清单】

typedef.h

#ifndef __TYPEDEF_H__

#define __TYPEDEF_H__

#include

#include

#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

#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 (*DestoryFunc)(Operation* thiz);

struct _Result

{

int val;

};

struct _Operation

{

ResultFunc result;

DestoryFunc destory;

};

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_destory(Operation *thiz)

{

if(thiz != NULL && thiz->destory != NULL)

{

thiz->destory(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

#include

#include

#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

#include

#include

#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

#include

#include

#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

#include

#include

#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_destory(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值